c#开发的运行在wm5.0环境中的sqlce数据库操作。

  1. 这是数据库的封装类:  
  2.   
  3. using System;  
  4. using System.Data;  
  5. using System.Data.Common;  
  6. using System.Data.SqlServerCe;  
  7. using System.Windows.Forms;  
  8. using System.IO;  
  9.   
  10. namespace testDB  
  11. {  
  12.     public class SQLCE  
  13.     {  
  14.          
  15.         public static SqlCeConnection conn = null;  
  16.         public static SqlCeTransaction trans = null;  
  17.         public static SqlCeCommand cmd = null;  
  18.   
  19.         public static string sData = System.IO.Path.GetDirectoryName(  
  20.             System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase) + "//test.sdf";  
  21.   
  22.        
  23.         public SQLCE()  
  24.         {  
  25.             // TODO: 在此处添加构造函数逻辑  
  26.         }  
  27.         /// 初始化数据库;  
  28.         public static bool Init()  
  29.         {  
  30.             try  
  31.             {  
  32.                 String sConn = "Data Source = " + sData;  
  33.                 if ((conn == null) || (conn.State == ConnectionState.Closed))  
  34.                 {  
  35.                     bool bCreate = false;  
  36.                     
  37.                     if (!File.Exists(SQLCE.sData))  
  38.                     {  
  39.                          
  40.                         String connStr = "Data Source = " + sData;  
  41.   
  42.                         SqlCeEngine engine = new SqlCeEngine(sConn);  
  43.                          
  44.                         engine.CreateDatabase();//创建数据库  
  45.                          
  46.                         engine.Dispose();  
  47.   
  48.                         bCreate = true;  
  49.                         
  50.                     }  
  51.                     conn = new SqlCeConnection(sConn);  
  52.                     cmd = new SqlCeCommand();  
  53.                     cmd.Connection = conn;  
  54.                     conn.Open();  
  55.                     if (conn.State == ConnectionState.Closed)  
  56.                     {  
  57.                         MessageBox.Show("连接数据库失败""提示",  
  58.                             MessageBoxButtons.OK, MessageBoxIcon.Exclamation,  
  59.                               MessageBoxDefaultButton.Button1);  
  60.                         return false;  
  61.                     }  
  62.                     
  63.                     if (bCreate)  
  64.                     {  
  65.                         SQLExec(Common.SQL_TEST);// 建表语句  
  66.                     }  
  67.                 }  
  68.                 MessageBox.Show("数据库初始化成功");  
  69.             }  
  70.             catch  
  71.             {  
  72.                 MessageBox.Show("连接数据库失败""提示", MessageBoxButtons.OK,  
  73.                     MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);  
  74.                 return false;  
  75.             }  
  76.             return true;  
  77.         }  
  78.   
  79.         /// 关闭数据库;  
  80.         public static bool Close()  
  81.         {  
  82.             try  
  83.             {  
  84.                 cmd.Connection.Close();  
  85.                 conn.Close();  
  86.                 return true;  
  87.             }  
  88.             catch  
  89.             {  
  90.                 return false;  
  91.             }  
  92.         }  
  93.   
  94.         /// 将查询结果封装到DataSet中  
  95.         /// <param name="sSQL"> sql 语句 </param>  
  96.         /// <param name="ds"> 调用方定义的DataSet </param>  
  97.         /// <returns> 成功返回true  失败返回false </returns>  
  98.         public static bool SQLQuery(string sSQL, ref DataSet ds)  
  99.         {  
  100.             try  
  101.             {  
  102.                 if (conn.State == ConnectionState.Closed)  
  103.                 {  
  104.                     conn.Open();  
  105.                 }  
  106.                 SqlCeDataAdapter adp = new SqlCeDataAdapter(sSQL, conn);  
  107.                 if (ds == null)  
  108.                 {  
  109.                     ds = new DataSet();  
  110.                 }  
  111.                 adp.Fill(ds);  
  112.             }  
  113.             catch  
  114.             {  
  115.                 MessageBox.Show("查询失败, SQL[ " + sSQL + " ]""提示",  
  116.                     MessageBoxButtons.OK, MessageBoxIcon.Exclamation,  
  117.                       MessageBoxDefaultButton.Button1);  
  118.                 return false;  
  119.             }  
  120.             return true;  
  121.         }  
  122.   
  123.         /// 将 查询结果封装到 lv 中;  
  124.         /// <param name="sSQL">sql语句</param>  
  125.         /// <param name="lv">外界定义的 ListView </param>  
  126.         /// <returns> 查询成功返回true  错误返回false </returns>  
  127.         public static bool SQLQuery(string sSQL, ref ListView lv)  
  128.         {  
  129.             try  
  130.             {  
  131.                 if (conn.State == ConnectionState.Closed)  
  132.                 {  
  133.                     conn.Open();  
  134.                 }  
  135.                 SqlCeDataAdapter adp = new SqlCeDataAdapter(sSQL, conn);  
  136.                 DataSet ds = new DataSet();  
  137.                 adp.Fill(ds);  
  138.                 lv.Items.Clear();  
  139.   
  140.                 int iRecordCount = ds.Tables[0].Rows.Count;  
  141.                 if (iRecordCount <= 0)  
  142.                 {  
  143.                     return true;  
  144.                 }  
  145.                 for (int i = 0; i < iRecordCount; i++)  
  146.                 {  
  147.                     string[] item = new string[ds.Tables[0].Columns.Count];  
  148.                     for (int j = 0; j < ds.Tables[0].Columns.Count; j++)  
  149.                     {  
  150.                         item[j] = ds.Tables[0].Rows[i][j].ToString();  
  151.                     }  
  152.                     lv.Items.Add(new ListViewItem(item));  
  153.                 }  
  154.                  //Common.AutoWidth(ref lv);  
  155.                 return true;  
  156.             }  
  157.             catch  
  158.             {  
  159.                 MessageBox.Show("查询失败, SQL[ " + sSQL + " ]""提示",  
  160.                     MessageBoxButtons.OK, MessageBoxIcon.Exclamation,  
  161.                        MessageBoxDefaultButton.Button1);  
  162.                 return false;  
  163.             }  
  164.         }  
  165.         /// 查询某表 的 某个字段的最大值;  
  166.         /// <param name="sTable">表名</param>  
  167.         /// <param name="sField">字段</param>  
  168.         public static int SQLMaxValue(string sTable, string sField)  
  169.         {  
  170.             string sSQL = "select max(" + sField + ") as MAXVALUE from " + sTable;  
  171.             try  
  172.             {  
  173.                 if (conn.State == ConnectionState.Closed)  
  174.                 {  
  175.                     conn.Open();  
  176.                 }  
  177.                 SqlCeDataAdapter adp = new SqlCeDataAdapter(sSQL, conn);  
  178.                 DataSet ds = new DataSet();  
  179.                 adp.Fill(ds);  
  180.                 if (ds.Tables.Count > 0)  
  181.                 {  
  182.                     if (ds.Tables[0].Rows.Count > 0)  
  183.                     {  
  184.                         return Common.StrToIntDef(ds.Tables[0].Rows[0][0].ToString(), 0);  
  185.                     }  
  186.                 }  
  187.             }  
  188.             catch  
  189.             {  
  190.                 MessageBox.Show("查询失败, SQL[ " + sSQL + " ]""提示",  
  191.                     MessageBoxButtons.OK, MessageBoxIcon.Exclamation,   
  192.                        MessageBoxDefaultButton.Button1);  
  193.                 return -1;  
  194.             }  
  195.             return 1;  
  196.         }  
  197.   
  198.         /// 查询单个值;  
  199.         /// <param name="sSQL">sql语句;</param>  
  200.         /// <returns>返回结果字符串</returns>  
  201.         public static string SQLValue(string sSQL)  
  202.         {  
  203.             try  
  204.             {  
  205.                 if (conn.State == ConnectionState.Closed)  
  206.                 {  
  207.                     conn.Open();  
  208.                 }  
  209.                 SqlCeDataAdapter adp = new SqlCeDataAdapter(sSQL, conn);  
  210.                 DataSet ds = new DataSet();  
  211.                 adp.Fill(ds);  
  212.                 if (ds.Tables.Count > 0)  
  213.                 {  
  214.                     if (ds.Tables[0].Rows.Count > 0)  
  215.                     {  
  216.                         return ds.Tables[0].Rows[0][0].ToString();  
  217.                     }  
  218.                 }  
  219.             }  
  220.             catch  
  221.             {  
  222.                 MessageBox.Show("查询失败, SQL[ " + sSQL + " ]""提示",   
  223.                      MessageBoxButtons.OK, MessageBoxIcon.Exclamation,  
  224.                        MessageBoxDefaultButton.Button1);  
  225.                 return "";  
  226.             }  
  227.             return "";  
  228.         }  
  229.   
  230.   
  231.         /// 不带事务的执行语句  
  232.         public static bool SQLExec(string sSQL)  
  233.         {  
  234.             try  
  235.             {  
  236.                 if (conn.State == ConnectionState.Closed)  
  237.                 {  
  238.                     conn.Open();  
  239.                 }  
  240.                 cmd.CommandText = sSQL;  
  241.                 cmd.ExecuteNonQuery();  
  242.             }  
  243.             catch  
  244.             {  
  245.                 MessageBox.Show("执行失败, SQL[ " + sSQL + " ]""提示",   
  246.                     MessageBoxButtons.OK, MessageBoxIcon.Exclamation,  
  247.                        MessageBoxDefaultButton.Button1);  
  248.                 return false;  
  249.             }  
  250.             return true;  
  251.         }  
  252.   
  253.   
  254.         /// 带事务的执行方式;  
  255.         /// <param name="sSQL">sql语句</param>  
  256.         /// <param name="bCommit">是否有事务提交</param>  
  257.   
  258.         public static bool SQLExec(string sSQL, bool bCommit)  
  259.         {  
  260.             try  
  261.             {  
  262.                 if (conn.State == ConnectionState.Closed)  
  263.                 {  
  264.                     conn.Open();  
  265.                 }  
  266.                 if (!bCommit)  
  267.                 {  
  268.                     trans = conn.BeginTransaction();  
  269.                     cmd.Transaction = trans;  
  270.                 }  
  271.                 cmd.CommandText = sSQL;  
  272.                 cmd.ExecuteNonQuery();  
  273.                 if (bCommit)  
  274.                 {  
  275.                     if (trans != null)  
  276.                     {  
  277.                         trans.Commit();  
  278.                         trans = null;  
  279.                     }  
  280.                 }  
  281.             }  
  282.             catch  
  283.             {  
  284.                 trans.Rollback();  
  285.                 MessageBox.Show("执行失败, SQL[ " + sSQL + " ]""提示",   
  286.                      MessageBoxButtons.OK, MessageBoxIcon.Exclamation,  
  287.                        MessageBoxDefaultButton.Button1);  
  288.                 return false;  
  289.             }  
  290.             return true;  
  291.         }  
  292.   
  293.   
  294.         //将 dataSet 传进去  再传出来;  
  295.         public static bool SQLExist(string sSQL)  
  296.         {  
  297.             try  
  298.             {  
  299.                 if (conn.State == ConnectionState.Closed)  
  300.                 {  
  301.                     conn.Open();  
  302.                 }  
  303.                 SqlCeDataAdapter adp = new SqlCeDataAdapter(sSQL, conn);  
  304.                 DataSet ds = new DataSet();  
  305.                 adp.Fill(ds);  
  306.                 if (ds.Tables.Count > 0)  
  307.                 {  
  308.                     if (ds.Tables[0].Rows.Count > 0)  
  309.                     {   //ds.Tables[0].Rows[0][0].ToString(); 数据格式;  
  310.                         return true;  
  311.                     }  
  312.                 }  
  313.             }  
  314.             catch  
  315.             {  
  316.                 MessageBox.Show("查询失败, SQL[ " + sSQL + " ]""提示",  
  317.                     MessageBoxButtons.OK, MessageBoxIcon.Exclamation,   
  318.                       MessageBoxDefaultButton.Button1);  
  319.                 return true;  
  320.             }  
  321.             return false;  
  322.         }  
  323.   
  324.         //执行存储过程  
  325.         public static bool SQLProc(string sProcName, params ProcParam[] pProcParams)  
  326.         {  
  327.             try  
  328.             {  
  329.                 if (conn.State == ConnectionState.Closed)  
  330.                 {  
  331.                     conn.Open();  
  332.                 }  
  333.                 cmd.CommandType = CommandType.StoredProcedure;  
  334.                 cmd.CommandText = sProcName;  
  335.                 cmd.Parameters.Clear();  
  336.   
  337.                 SqlCeParameter para = cmd.Parameters.Add("@RETURN_VALUE", SqlDbType.Int);  
  338.                 para.Direction = ParameterDirection.ReturnValue;  
  339.   
  340.                 for (int i = 0; i < pProcParams.Length; i++)  
  341.                 {  
  342.                     para = cmd.Parameters.Add(pProcParams[i].ParamName, pProcParams[i].ParamType);  
  343.                     para.Value = pProcParams[i].ParamValue;  
  344.                 }  
  345.                 para = cmd.Parameters.Add("@err_msg", SqlDbType.VarChar, 80);  
  346.                 para.Direction = ParameterDirection.Output;  
  347.   
  348.                 cmd.ExecuteNonQuery();  
  349.                 if (Common.StrToIntDef(cmd.Parameters["@RETURN_VALUE"].Value.ToString(), -1) < 0)  
  350.                 {  
  351.                     MessageBox.Show(cmd.Parameters["@err_msg"].Value.ToString(), "提示",   
  352.                         MessageBoxButtons.OK, MessageBoxIcon.Exclamation,   
  353.                           MessageBoxDefaultButton.Button1);  
  354.                     return false;  
  355.                 }  
  356.             }  
  357.             catch (Exception e)  
  358.             {  
  359.                 MessageBox.Show(e.Message + "调用存储过程失败""提示", MessageBoxButtons.OK,   
  360.                     MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);  
  361.                 return false;  
  362.             }  
  363.             return true;  
  364.         }  
  365.   
  366.         public static void CmbAdd(ref System.Windows.Forms.ComboBox cmb, string sSQL)  
  367.         {  
  368.             try  
  369.             {  
  370.                 if (conn.State == ConnectionState.Closed)  
  371.                 {  
  372.                     conn.Open();  
  373.                 }  
  374.                 SqlCeDataAdapter adp = new SqlCeDataAdapter(sSQL, conn);  
  375.                 DataSet ds = new DataSet();  
  376.                 adp.Fill(ds);  
  377.                 cmb.Items.Clear();  
  378.                 if (ds.Tables.Count > 0)  
  379.                 {  
  380.                     for (int i = 0; i < ds.Tables[0].Rows.Count; i++)  
  381.                     {  
  382.                         cmb.Items.Add(ds.Tables[0].Rows[i][0].ToString() + "-" + ds.Tables[0].Rows[i][1].ToString());  
  383.                     }  
  384.                     cmb.SelectedIndex = 0;  
  385.                 }  
  386.             }  
  387.             catch  
  388.             {  
  389.                 MessageBox.Show("查询失败, SQL[ " + sSQL + " ]""提示",  
  390.                   MessageBoxButtons.OK, MessageBoxIcon.Exclamation,  
  391.                   MessageBoxDefaultButton.Button1);  
  392.             }  
  393.         }  
  394.   
  395.         public static void CmbAdd(ref System.Windows.Forms.ComboBox cmb, string sSQL, bool bBlank)  
  396.         {  
  397.             try  
  398.             {  
  399.                 if (conn.State == ConnectionState.Closed)  
  400.                 {  
  401.                     conn.Open();  
  402.                 }  
  403.                 SqlCeDataAdapter adp = new SqlCeDataAdapter(sSQL, conn);  
  404.                 DataSet ds = new DataSet();  
  405.                 adp.Fill(ds);  
  406.                 cmb.Items.Clear();  
  407.                 if (ds.Tables.Count > 0)  
  408.                 {  
  409.                     if (bBlank)  
  410.                     {  
  411.                         cmb.Items.Add("");  
  412.                     }  
  413.                     for (int i = 0; i < ds.Tables[0].Rows.Count; i++)  
  414.                     {  
  415.                         cmb.Items.Add(ds.Tables[0].Rows[i][0].ToString() + "-" +  
  416.                             ds.Tables[0].Rows[i][1].ToString());  
  417.                     }  
  418.                     cmb.SelectedIndex = 0;  
  419.                 }  
  420.             }  
  421.             catch  
  422.             {  
  423.                 MessageBox.Show("查询失败, SQL[ " + sSQL + " ]""提示",   
  424.                 MessageBoxButtons.OK, MessageBoxIcon.Exclamation,   
  425.                 MessageBoxDefaultButton.Button1);  
  426.             }  
  427.         }  
  428.   
  429.     }  
  430.   
  431.     public class ProcParam  
  432.     {  
  433.         public string ParamName;  
  434.         public SqlDbType ParamType;  
  435.         public object ParamValue;  
  436.   
  437.         public ProcParam(string Name, SqlDbType Type, object Value)  
  438.         {  
  439.             ParamName = Name;  
  440.             ParamType = Type;  
  441.             ParamValue = Value;  
  442.         }  
  443.     }  
  444. }  
  445.   
  446. 源码上传了!  

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值