database name with underscore issue

Answered database name with underscore issue

  • Friday, July 20, 2012 3:26 PM
     
      Has Code

    Hi,

    I am using sql smo on trying to query out database size. Everything works, but when I have a DB with an _ in the name ie) 123_mydb, it says the db cannot be found. When I run sql profiler, I can run the query being passed in and it works just fine. I am at a loss as I tried escaping unless there is a specific way to escape with smo, but the db name is being passed in as a parameter. Here is my code on my console app then my class below

                foreach (var item in mssql.mssql.MSSQLdatabaseSize("123_test""192.168.1.10"))
                {
                    Console.WriteLine(item.ToString());
                }

    my class

    public static List<string> MSSQLdatabaseSize (string database, string sqlServer)
    		{
    if (sqlServer != null && sqlServer != "")
    			{
    				try
    				{
     
    					String sqlServerLogin = "testlogin"//will be set in web.config or windows auth
    					String password = "testpassword"//will be set in web.config or windows auth
    					String remoteSvrName = sqlServer;
     
    					ServerConnection srvConn = new ServerConnection(remoteSvrName);
    					srvConn.LoginSecure = false//not using windows auth
    					srvConn.Login = sqlServerLogin;
    					srvConn.Password = password;
    					Server srv = new Server(srvConn);
     
    					Database dbSize = srv.Databases[database];
    					List<string> lSize = new List<string>();
     
                     
     
                        if (srv.Databases.Contains(database))
                        {
    						lSize.Add("DataSpaceusage(KB) "+dbSize.DataSpaceUsage.ToString());
    						lSize.Add("IndexSpaceUsage(KB) " + dbSize.IndexSpaceUsage.ToString());
    						lSize.Add("SpaceAvailable(KB) " + dbSize.SpaceAvailable.ToString());
    						lSize.Add("Total DB Size w/ log (MB) " + dbSize.Size.ToString());
    						srvConn.Disconnect();
    						return lSize;
     
                        }
    					
     
    					if (!srv.Databases.Contains(database))
    					{
    					
    						List<string> noDB = new List<string>();
    						noDB.Add("No database found");
    						srvConn.Disconnect();
    						return noDB;
    					}
     
    				}
    				catch (Exception ex)
    				{
    					List<string> er = new List<string>();
    					er.Add(ex.Message);
    					return er;
    					
    				}
    

All Replies

  • Friday, July 20, 2012 3:37 PM
     
     Answered Has Code

    Hello,

    I am pretty sure that not the underscore causes the problem; it's more that the name starts with a numeric. All SQL Server object names have to start with a letter, numerics and special characters are not allowed. If you even us a numeric / special character you have to set the name in brackets [ ], so try:

    foreach (var item in mssql.mssql.MSSQLdatabaseSize("[123_test]", "192.168.1.10"))
    


    Olaf Helper
    * cogito ergo sum * errare humanum est * quote erat demonstrandum *
    Wenn ich denke, ist das ein Fehler und das beweise ich täglich
    Blog Xing

    • Marked As Answer by jrsqlguy Friday, July 20, 2012 4:04 PM
    •  
  • Friday, July 20, 2012 3:57 PM
     
     

    So, my fault..it seems someone changed my permissions on my service account...thanks though for your time

    • Edited by jrsqlguy Friday, July 20, 2012 4:04 PM
    •