CatalogsDB.cs
上传用户:autodoor
上传日期:2022-08-04
资源大小:9973k
文件大小:4k
源码类别:

.net编程

开发平台:

Others

  1. using System;
  2. using System.Data.SqlClient;
  3. using System.Data;
  4. using qminoa.Common.Data;
  5. namespace qminoa.DA
  6. {
  7. public class CatalogsDB
  8. {
  9. private string conStr ;
  10. private SqlConnection con ;
  11. private SqlDataAdapter commandAdp ;
  12. public CatalogsDB()
  13. {
  14. conStr = System.Configuration.ConfigurationSettings.AppSettings.Get("ConnectionString");
  15. con = new SqlConnection(conStr);
  16. commandAdp = new SqlDataAdapter();
  17. }
  18. private string paramChg(string str)
  19. {
  20. str = "@"+str;
  21. return str;
  22. }
  23. public DataTable GetTab(string tabid)
  24. {
  25. SqlDataAdapter Command = new SqlDataAdapter();
  26. Command.SelectCommand = new SqlCommand();
  27. DataSet ds_Tabs = new DataSet();
  28. Command.SelectCommand.Connection = con;
  29. Command.SelectCommand.CommandText ="GetsysTab";
  30. Command.SelectCommand.CommandType = CommandType.StoredProcedure;
  31. SqlParameter para_tabid = new SqlParameter("@tabid",SqlDbType.VarChar,20);
  32. para_tabid.Value = tabid;
  33. Command.SelectCommand.Parameters.Add(para_tabid);
  34. Command.Fill(ds_Tabs,"thisTab");
  35. return ds_Tabs.Tables["thisTab"];
  36. }
  37. public DataSet GetCatalogsData()
  38. {
  39. SqlCommand command = new SqlCommand();
  40. command.CommandText = "select * from sysTab order by tabid";
  41. command.Connection = con;
  42. commandAdp.SelectCommand = command;
  43. DataSet data = new DataSet();
  44. commandAdp.Fill(data);
  45. return data;
  46. }
  47. public bool DeleteCatalog(int pkid)
  48. {
  49. SqlCommand command = new SqlCommand();
  50. command.CommandText = "DeleteCatalogs";
  51. command.CommandType = CommandType.StoredProcedure;
  52. SqlParameter param = new SqlParameter(paramChg(CatalogData.PK_FIELD),SqlDbType.Int);
  53. param.Value = pkid;
  54. command.Parameters.Add(param);
  55. command.Connection = con;
  56. con.Open();
  57. int i = command.ExecuteNonQuery();
  58. con.Close();
  59. if(i==0)
  60. {
  61. return false;
  62. }
  63. else
  64. {
  65. return true;
  66. }
  67. }
  68. public bool UpdateCatalog(CatalogData catalogData)
  69. {
  70. commandAdp.UpdateCommand = GetCommand();
  71. commandAdp.UpdateCommand.Parameters.Add(new SqlParameter(paramChg(CatalogData.PK_FIELD),SqlDbType.Int));
  72. commandAdp.UpdateCommand.Parameters[paramChg(CatalogData.PK_FIELD)].SourceColumn = CatalogData.PK_FIELD;
  73. commandAdp.UpdateCommand.CommandText = "UpdateCatalogs";
  74. commandAdp.Update(catalogData,CatalogData.CATALOG_TABLE_NAME);
  75. if(catalogData.HasErrors)
  76. {
  77. catalogData.Tables[CatalogData.CATALOG_TABLE_NAME].GetErrors()[0].ClearErrors();
  78. return false;
  79. }
  80. else
  81. {
  82. catalogData.AcceptChanges();
  83. return true;
  84. }
  85. }
  86.  public bool CreateCatalog(CatalogData catalogData)
  87.  {
  88.  commandAdp.InsertCommand = GetCommand();
  89.  commandAdp.InsertCommand.CommandText = "InsertCatalog";
  90.  commandAdp.Update(catalogData,CatalogData.CATALOG_TABLE_NAME);
  91.  if(catalogData.HasErrors)
  92.  {
  93.  catalogData.Tables[CatalogData.CATALOG_TABLE_NAME].GetErrors()[0].ClearErrors();
  94.  return false;
  95.  }
  96.  else
  97.  {
  98.  catalogData.AcceptChanges();
  99.  return true;
  100.  }
  101.  }
  102. private SqlCommand GetCommand()
  103. {
  104. SqlCommand command = new SqlCommand();
  105. command.Connection = con;
  106. command.CommandType = CommandType.StoredProcedure;
  107. SqlParameterCollection param = command.Parameters;
  108. param.Add(new SqlParameter(paramChg(CatalogData.TABID_FIELD),SqlDbType.VarChar));
  109. param.Add(new SqlParameter(paramChg(CatalogData.INDEXID_FIELD),SqlDbType.VarChar));
  110. param.Add(new SqlParameter(paramChg(CatalogData.NAME_FIELD),SqlDbType.VarChar));
  111. param.Add(new SqlParameter(paramChg(CatalogData.DESCRIPTION_FIELD),SqlDbType.VarChar));
  112. param.Add(new SqlParameter(paramChg(CatalogData.URL_FIELD),SqlDbType.VarChar));
  113. param[paramChg(CatalogData.TABID_FIELD)].SourceColumn = CatalogData.TABID_FIELD;
  114. param[paramChg(CatalogData.INDEXID_FIELD)].SourceColumn = CatalogData.INDEXID_FIELD;
  115. param[paramChg(CatalogData.NAME_FIELD)].SourceColumn = CatalogData.NAME_FIELD;
  116. param[paramChg(CatalogData.DESCRIPTION_FIELD)].SourceColumn = CatalogData.DESCRIPTION_FIELD;
  117. param[paramChg(CatalogData.URL_FIELD)].SourceColumn = CatalogData.URL_FIELD;
  118. return command;
  119. }
  120. }
  121. }