backup script for a table in MSSQL 2005

Please support our MS SQL advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2007
Posts: 14
Reputation: bhakti.thakkar is an unknown quantity at this point 
Solved Threads: 1
bhakti.thakkar bhakti.thakkar is offline Offline
Newbie Poster

backup script for a table in MSSQL 2005

 
0
  #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
Reply With Quote Quick reply to this message  
Join Date: Nov 2006
Posts: 224
Reputation: bugmenot is an unknown quantity at this point 
Solved Threads: 31
bugmenot bugmenot is offline Offline
Posting Whiz in Training

Re: backup script for a table in MSSQL 2005

 
0
  #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. }



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 Quick reply to this message  
Join Date: Sep 2007
Posts: 14
Reputation: bhakti.thakkar is an unknown quantity at this point 
Solved Threads: 1
bhakti.thakkar bhakti.thakkar is offline Offline
Newbie Poster

Re: backup script for a table in MSSQL 2005

 
0
  #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. }



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 Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the MS SQL Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC