Wednesday, March 21, 2012

generating script with SMO

I am taking my table names and colums from xml file and then generating the script and executing it.I have problem creating the script for incerting table with multiple colums.Maybe the problem is that I am using a for loop to get all column names from a array string.Has anyone idea why it is not working? When I try insert of a table without loop it is wirking!Here is the code :
Server server = new Server(); // Create table in my personal database Database db = server.Databases["new"]; // Make Sure Constraint is Scripted ScriptingOptions so = new ScriptingOptions(); so.IncludeHeaders = true; so.SchemaQualify = true; // Create In-Memory Table Table newTable = new Table(db, Names[0].ToString()); for (int i = 1; i < Names.Length; i++) { Column newColumn = new Column(newTable,NamesIdea); newTable.Columns.Add(newColumn); } foreach (string s in newTable.Script(so)) /// Error generating the script { myTableScript = myTableScript + s; } db.ExecuteNonQuery(myTableScript); //disconnect form Database connection.Close();

Could you please metion the full error you are getting.

Thanks,
Kuntal

|||I fixed the problem.I haven't set the column format and that's why the script was not correctly generated.The loop is functioning now ok. Here is the edit line :Column newColumn = new Column(newTable, attr.Name, DataType.NVarChar(50));Now I can easily take the table and column names from the xml file and directly put them without using array strings.Thanks for yor time :) Have a nice day !

No comments:

Post a Comment