[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

problem creating sql script using SMO

Posted on 2013-06-24
3
Medium Priority
?
426 Views
Last Modified: 2013-10-07
I am using SMO to create and write a sql file.
I am getting the following error:
Thanks in advance for any help given.
Use of unassigned local variable 'Tables' Line 208
foreach (var tbl in Tables) // error line 208
                    {
                        db.Tables[tbl].EnumScript(options);
                    }

Open in new window

Cannot implicitly convert type 'Microsoft.SqlServer.Management.Smo.TableCollection' to 'string[]' Line 185
Tables = tables;// error line 185

Open in new window

For clarity, the full code is listed below:
Response.Write(MyAppCaaaMisc2.divStart2);
            try
            {       
                if (!IsPostBack)
                {
                    //<!-- Start Page Content -->
                    string Dbase = Request.QueryString["database"];// aaaMisc2.aspx?database=Cams2
                    string Tbase = Request.QueryString["table"];
                    string database_tb = null;
                    string server_tb = null;
                    string user_tb = null;
                    string pswrd_tb = null;
                    string[] Tables;
                    string FileName = @"C:\TableScriptWithDependencies.sql";
                    connectionString = Session["strConnection"].ToString();// line 168 Connection details etc
                    Database database = SQLTableDatabaseClass.SinglDatabase(connectionString, Dbase.ToString());
                    string connstr = "@" + database; 
                    MyAppCaaaMisc2.errorLabel1 = "<font color='red'>" + "174:FL:" + "</font>" + connstr + ";  connectionString:" + connectionString + "#";
                    MyAppdivEL1();
                    string[] str1 = connstr.Split(']');
                    database_tb = str1[0].Split('[')[1];
                    connstr = "@" + connectionString; // detailsLabel.Text;
                    str1 = connstr.Split(';');
                    server_tb = str1[0].Split('=')[1];
                    user_tb = str1[1].Split('=')[1];
                    pswrd_tb = str1[2].Split('=')[1];
                    MyAppCaaaMisc2.errorLabel2 = "<font color='red'>" + "183:FL:" + "</font>" + "database_tb:" + database_tb + "    server_tb:" + server_tb + "    user_tb:" + user_tb + "    pswrd_tb:" + pswrd_tb + "    connectionString:" + connectionString + "#";
                    MyAppdivEL2();
                    TableCollection tables = SQLTableDatabaseClass.GetTablesFromDatabase(connectionString, database_tb);
                    //public static void Backup(string FileName, string[] Tables)
                    //<!-- Start Page Content -->
                    Tables = tables;// error line 185
                    StringBuilder sb = new StringBuilder();
                    Server srv = new Server(new Microsoft.SqlServer.Management.Common.ServerConnection(server_tb, user_tb, pswrd_tb));    
                    Database db = srv.Databases[database_tb];  
                    Response.Write("<br/><br/><br/><br/>");
                    foreach (User user in db.Users)
                    {
                        Response.Write("User: " + user.Name);
                        Response.Write("<br/>");
                        Response.Write("Login: " + user.Login);
                        Response.Write("<br/>");
                        Response.Write("Type: " + user.UserType);
                    }
                    ScriptingOptions options = new ScriptingOptions();
                    options.ScriptData = true;
                    options.ScriptDrops = false;
                    options.FileName = FileName;
                    options.EnforceScriptingOptions = true;
                    options.ScriptSchema = true;
                    options.IncludeHeaders = true;
                    options.AppendToFile = true;
                    options.Indexes = true;
                    options.WithDependencies = true;
                    foreach (var tbl in Tables) // error line 208
                    {
                        db.Tables[tbl].EnumScript(options);
                    }
                    //<!-- End Page Content -->
                }// End !IsPostBack
                else
                {
                //<!-- Start Page Content -->
     
                //<!-- End Page Content -->
                }
            } // End try
            catch (Exception ex)
            {
                MyAppCaaaMisc2.errorLabel4 = "<font color='red'>" + "err:220:PL:" + "</font>" + MyAppCaaaMisc2.errLabel + "&nbsp;&nbsp;" + ex.ToString();
                MyAppdivEL4();
            } // End catch
            Response.Write(MyAppCaaaMisc2.divEnd);

Open in new window

0
Comment
Question by:homeshopper
  • 3
3 Comments
 

Author Comment

by:homeshopper
ID: 39271057
I have tried the following:
for (int i = 0; i < tables.Count; i++)
                    {
                        Response.Write("(" + i + ")" + tables[i].ToString() + "#<br/>");
                        Tables[i] = tables[i].ToString();
                    }

Get following error at line 190 Tables[i] = tables[i].ToString();
System.NullReferenceException: Object reference not set to an instance of an object.
Response.Write("(" + i + ")" + tables[i].ToString() + "#<br/>");

Open in new window

does display table names.
0
 

Accepted Solution

by:
homeshopper earned 0 total points
ID: 39272462
Get following error at line 190:
System.NullReferenceException: Object reference not set to an instance of an object.
line 190 Tables[i] = tables[i].ToString();

Open in new window

Complete code below:
for (int i = 0; i < tables.Count; i++)
                    {
                        Response.Write("(" + i + ")" + tables[i].ToString() + "#<br/>");
                        Tables[i] = tables[i].ToString();
                    }

Open in new window

0
 

Author Closing Comment

by:homeshopper
ID: 39551619
I have now solved the problem
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

831 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question