User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: Sep 2007
Posts: 13
Reputation: bhakti.thakkar is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
bhakti.thakkar bhakti.thakkar is offline Offline
Newbie Poster

backup script for a table in MSSQL 2005

  #1  
Sep 5th, 2007
hi all,
Very new this forum. i am using MSSQL 2005 and i want to create a backup script for a table [create and insert statements] without using mssql datapublishing wizard 2005. is it possible?? how can i do it

Best Regard
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Nov 2006
Posts: 174
Reputation: bugmenot is on a distinguished road 
Rep Power: 2
Solved Threads: 26
bugmenot bugmenot is offline Offline
Junior Poster

Re: backup script for a table in MSSQL 2005

  #2  
Sep 5th, 2007
  1. al = new ArrayList();
  2. dt = myHelper.GetDS("SELECT name FROM sys.tables ").Tables[0];
  3. foreach (DataRow r in dt.Rows)
  4. {
  5. al.Add(r["name"].ToString());
  6. }
  7.  
  8. foreach (string i in al)
  9. {
  10. DataTable proc = myHelper.GetDS("select * from " + i).Tables[0];
  11. Tables.Add(dbToInsert(proc, i));
  12. }
  13.  
  14.  
  15. private static string dbToInsert(DataTable proc, string name)
  16. {
  17. string insert = "";
  18. if (isIndent(name)) insert = "SET IDENTITY_INSERT " + name + " ON ; " + "\n";
  19.  
  20. string colList = "";
  21.  
  22. foreach (DataColumn col in proc.Columns)
  23. {
  24. colList += col.ColumnName + ", ";
  25. }
  26. colList = colList.Substring(0, colList.Length - 2);
  27.  
  28. foreach (DataRow row in proc.Rows)
  29. {
  30. insert += "insert into " + name + " (" + colList + ") values(";
  31.  
  32. for (int i = 0; i <= proc.Columns.Count - 1; i += 1)
  33. {
  34. string tempstr = row[i].ToString();
  35. tempstr = tempstr.Replace("'", "");
  36.  
  37. if (tempstr == "")
  38. insert += "NULL, ";
  39. else if (proc.Columns[i].DataType == Type.GetType("System.String") ||
  40. proc.Columns[i].DataType == Type.GetType("System.DateTime"))
  41. insert += "'" + tempstr + "', ";
  42. else if (tempstr == "False" || tempstr == "True")
  43. insert += (tempstr == "False" ? 0 : 1) + ", ";
  44. else
  45. insert += tempstr + ", ";
  46. }
  47. insert = insert.Substring(0, insert.Length - 2) + "); " + "\n";
  48. }
  49. if (isIndent(name)) insert += "SET IDENTITY_INSERT " + name + " OFF ;" + "\n";
  50.  
  51. return insert;
  52. }
  53.  
  54. private static bool isIndent(string name)
  55. {
  56. string[] str = {
  57. "AdminUsers", "ContractorToService", "ListingsToIndustries", "MaintenanceLimits",
  58. "ProjectsToIndustries", "ZipcodeToRegion","UserFavorates","sysdiagrams","ListingPrice"
  59. };
  60. foreach (string s in str)
  61. {
  62. if (s == name) return false;
  63. }
  64. return true;
  65. }
  66.  



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 .
Reply With Quote  
Join Date: Sep 2007
Posts: 13
Reputation: bhakti.thakkar is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
bhakti.thakkar bhakti.thakkar is offline Offline
Newbie Poster

Re: backup script for a table in MSSQL 2005

  #3  
Sep 5th, 2007
thanks a lot for your help. let me try it out and see what it returns.


Originally Posted by bugmenot View Post
  1. al = new ArrayList();
  2. dt = myHelper.GetDS("SELECT name FROM sys.tables ").Tables[0];
  3. foreach (DataRow r in dt.Rows)
  4. {
  5. al.Add(r["name"].ToString());
  6. }
  7.  
  8. foreach (string i in al)
  9. {
  10. DataTable proc = myHelper.GetDS("select * from " + i).Tables[0];
  11. Tables.Add(dbToInsert(proc, i));
  12. }
  13.  
  14.  
  15. private static string dbToInsert(DataTable proc, string name)
  16. {
  17. string insert = "";
  18. if (isIndent(name)) insert = "SET IDENTITY_INSERT " + name + " ON ; " + "\n";
  19.  
  20. string colList = "";
  21.  
  22. foreach (DataColumn col in proc.Columns)
  23. {
  24. colList += col.ColumnName + ", ";
  25. }
  26. colList = colList.Substring(0, colList.Length - 2);
  27.  
  28. foreach (DataRow row in proc.Rows)
  29. {
  30. insert += "insert into " + name + " (" + colList + ") values(";
  31.  
  32. for (int i = 0; i <= proc.Columns.Count - 1; i += 1)
  33. {
  34. string tempstr = row[i].ToString();
  35. tempstr = tempstr.Replace("'", "");
  36.  
  37. if (tempstr == "")
  38. insert += "NULL, ";
  39. else if (proc.Columns[i].DataType == Type.GetType("System.String") ||
  40. proc.Columns[i].DataType == Type.GetType("System.DateTime"))
  41. insert += "'" + tempstr + "', ";
  42. else if (tempstr == "False" || tempstr == "True")
  43. insert += (tempstr == "False" ? 0 : 1) + ", ";
  44. else
  45. insert += tempstr + ", ";
  46. }
  47. insert = insert.Substring(0, insert.Length - 2) + "); " + "\n";
  48. }
  49. if (isIndent(name)) insert += "SET IDENTITY_INSERT " + name + " OFF ;" + "\n";
  50.  
  51. return insert;
  52. }
  53.  
  54. private static bool isIndent(string name)
  55. {
  56. string[] str = {
  57. "AdminUsers", "ContractorToService", "ListingsToIndustries", "MaintenanceLimits",
  58. "ProjectsToIndustries", "ZipcodeToRegion","UserFavorates","sysdiagrams","ListingPrice"
  59. };
  60. foreach (string s in str)
  61. {
  62. if (s == name) return false;
  63. }
  64. return true;
  65. }
  66.  



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 .
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb MS SQL Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the MS SQL Forum

All times are GMT -4. The time now is 12:41 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC