•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the MS SQL section within the Web Development category of DaniWeb, a massive community of 392,091 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,908 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our MS SQL advertiser:
Views: 3486 | Replies: 2
![]() |
•
•
Join Date: Nov 2006
Posts: 174
Reputation:
Rep Power: 2
Solved Threads: 26
c# Syntax (Toggle Plain Text)
al = new ArrayList(); dt = myHelper.GetDS("SELECT name FROM sys.tables ").Tables[0]; foreach (DataRow r in dt.Rows) { al.Add(r["name"].ToString()); } foreach (string i in al) { DataTable proc = myHelper.GetDS("select * from " + i).Tables[0]; Tables.Add(dbToInsert(proc, i)); } private static string dbToInsert(DataTable proc, string name) { string insert = ""; if (isIndent(name)) insert = "SET IDENTITY_INSERT " + name + " ON ; " + "\n"; string colList = ""; foreach (DataColumn col in proc.Columns) { colList += col.ColumnName + ", "; } colList = colList.Substring(0, colList.Length - 2); foreach (DataRow row in proc.Rows) { insert += "insert into " + name + " (" + colList + ") values("; for (int i = 0; i <= proc.Columns.Count - 1; i += 1) { string tempstr = row[i].ToString(); tempstr = tempstr.Replace("'", ""); if (tempstr == "") insert += "NULL, "; else if (proc.Columns[i].DataType == Type.GetType("System.String") || proc.Columns[i].DataType == Type.GetType("System.DateTime")) insert += "'" + tempstr + "', "; else if (tempstr == "False" || tempstr == "True") insert += (tempstr == "False" ? 0 : 1) + ", "; else insert += tempstr + ", "; } insert = insert.Substring(0, insert.Length - 2) + "); " + "\n"; } if (isIndent(name)) insert += "SET IDENTITY_INSERT " + name + " OFF ;" + "\n"; return insert; } private static bool isIndent(string name) { string[] str = { "AdminUsers", "ContractorToService", "ListingsToIndustries", "MaintenanceLimits", "ProjectsToIndustries", "ZipcodeToRegion","UserFavorates","sysdiagrams","ListingPrice" }; foreach (string s in str) { if (s == name) return false; } return true; }
This code will do it. GetDs simply returns the dataset from the query, and dbToInsert will go through each row and dump the correct row data as an insert statement. IsIdent dumps back if the tables have an identity column. I should mention this will only work if the table has a primary key. I wrote this a long time ago so you may have to play with it, but itll get you started.
Let me know if you need any help at dested@gmail.com .
•
•
Join Date: Sep 2007
Posts: 13
Reputation:
Rep Power: 1
Solved Threads: 0
thanks a lot for your help. let me try it out and see what it returns.
•
•
•
•
c# Syntax (Toggle Plain Text)
al = new ArrayList(); dt = myHelper.GetDS("SELECT name FROM sys.tables ").Tables[0]; foreach (DataRow r in dt.Rows) { al.Add(r["name"].ToString()); } foreach (string i in al) { DataTable proc = myHelper.GetDS("select * from " + i).Tables[0]; Tables.Add(dbToInsert(proc, i)); } private static string dbToInsert(DataTable proc, string name) { string insert = ""; if (isIndent(name)) insert = "SET IDENTITY_INSERT " + name + " ON ; " + "\n"; string colList = ""; foreach (DataColumn col in proc.Columns) { colList += col.ColumnName + ", "; } colList = colList.Substring(0, colList.Length - 2); foreach (DataRow row in proc.Rows) { insert += "insert into " + name + " (" + colList + ") values("; for (int i = 0; i <= proc.Columns.Count - 1; i += 1) { string tempstr = row[i].ToString(); tempstr = tempstr.Replace("'", ""); if (tempstr == "") insert += "NULL, "; else if (proc.Columns[i].DataType == Type.GetType("System.String") || proc.Columns[i].DataType == Type.GetType("System.DateTime")) insert += "'" + tempstr + "', "; else if (tempstr == "False" || tempstr == "True") insert += (tempstr == "False" ? 0 : 1) + ", "; else insert += tempstr + ", "; } insert = insert.Substring(0, insert.Length - 2) + "); " + "\n"; } if (isIndent(name)) insert += "SET IDENTITY_INSERT " + name + " OFF ;" + "\n"; return insert; } private static bool isIndent(string name) { string[] str = { "AdminUsers", "ContractorToService", "ListingsToIndustries", "MaintenanceLimits", "ProjectsToIndustries", "ZipcodeToRegion","UserFavorates","sysdiagrams","ListingPrice" }; foreach (string s in str) { if (s == name) return false; } return true; }
This code will do it. GetDs simply returns the dataset from the query, and dbToInsert will go through each row and dump the correct row data as an insert statement. IsIdent dumps back if the tables have an identity column. I should mention this will only work if the table has a primary key. I wrote this a long time ago so you may have to play with it, but itll get you started.
Let me know if you need any help at dested@gmail.com .
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb MS SQL Marketplace
- Adding a field to an existing MSSQL table with SQL query (MS SQL)
- ORDER BY - join column (MS SQL)
- MSSQL and ASP Query (ASP.NET)
- Tablet pc edition 2005 question (Windows NT / 2000 / XP / 2003)
- SQL 2005 won't start after critical update reboot (MS SQL)
- thnall problem (Viruses, Spyware and other Nasties)
- in over my head LOST welcha? wininet? (Viruses, Spyware and other Nasties)
- backup files to cdr ? (Python)
- shell scripting help with backup script (Shell Scripting)
- Help with a script (PHP)
Other Threads in the MS SQL Forum
- Previous Thread: SQL Server 2000.............Help Needed
- Next Thread: Sql Query


Linear Mode