微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

WinCE.NET 上SQLServer CE3.0 C#数量I/O操作方法集

WinCE.NET 上sqlServer CE3.0 C#数量I/O操作方法

using  System;

using  System.Data;

using  System.Data.Common;

using  System.Data.sqlServerCe;

using  System.Windows.Forms;

using  System.IO;



namespace  SmartTerm.Pub

{

 
/// <summary>

 
/// sqlCE 的摘要说明。

 
/// </summary>

 public class sqlCE

 
{

  
public static sqlCeEngine eng = null;

  
public static sqlCeConnection conn = null;

  
public static sqlCeTransaction trans = null;

  
public static sqlCeCommand cmd = null;


  
public static string sData = @" ";

  
public static string sBackup = @"";

  
public static string sConn = @" ";


  
public sqlCE()

  
{

   
//

   
// Todo: 在此处添加构造函数逻辑

   
//

  }


  
public static bool Init()

  
{

   
try

   
{

    
if ((conn == null|| (conn.State == ConnectionState.Closed))

    
{

     
bool bCreate = false;

     
if (!File.Exists(sData))

     
{

      eng 
= new sqlCeEngine(sConn);

      eng.CreateDatabase();

      bCreate 
= true;

     }

     conn 
= new sqlCeConnection(sConn);

     cmd 
= new sqlCeCommand();

     cmd.Connection 
= conn;

     conn.open();

     
if (conn.State == ConnectionState.Closed)

     
{

      MessageBox.Show(
"连接数据库失败""提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);

      
return false;

     }

     conn.Close();

     
if (bCreate)

     
{

      sqlExec(Common.sql_OPERINFO);

      sqlExec(Common.sql_ORDERBILL);

      sqlExec(Common.sql_ORDERITEM);

      sqlExec(Common.sql_BUYBILL);

      sqlExec(Common.sql_BUYITEM);

     }

    }

   }

   
catch

   
{

    MessageBox.Show(
"连接数据库失败", MessageBoxDefaultButton.Button1);

    
return false;

   }

   
return true;

  }


  
public static bool Close()

  
{

   
try

   
{

    conn.Close();

    
return true;

   }

   
catch

   
{

    
return false;

   }

  }


  
public static bool sqlQuery(string ssqlref DataSet ds)

  
{

   
try

   
{

    
if (conn.State == ConnectionState.Closed)

    
{

     conn.open();

    }

    sqlCeDataAdapter adp 
= new sqlCeDataAdapter(ssql, conn);

    
if (ds == null)

    
{

     ds 
= new DataSet();

    }

    adp.Fill(ds);

   }

   
catch

   
{

    MessageBox.Show(
"查询失败, sql" + ssql + " ]", MessageBoxDefaultButton.Button1);

    
return false;

   }

   
return true;

  }

  

  
public static bool sqlQuery(string ssqlref ListView lv)

  
{

   
try

   
{

    
if (conn.State == ConnectionState.Closed)

    
{

     conn.open();

    }

    sqlCeDataAdapter adp 
= new sqlCeDataAdapter(ssql, conn);

    DataSet ds 
= new DataSet();

    adp.Fill(ds);

    lv.Items.Clear();


    
int iRecordCount = ds.Tables[0].Rows.Count;

    
if (iRecordCount <= 0)

    
{

     
return true;

    }

    
for (int i = 0; i < iRecordCount; i ++)

    
{

     
string[] item = new string[ds.Tables[0].Columns.Count];

     
for (int j = 0; j < ds.Tables[0].Columns.Count; j ++)

     
{

      item[j] 
= ds.Tables[0].Rows[i][j].ToString();

     }

     lv.Items.Add(
new ListViewItem(item));

    }

    Common.AutoWidth(
ref lv);

    
return true;

   }

   
catch

   
{

    MessageBox.Show(
"查询失败, MessageBoxDefaultButton.Button1);

    
return false;

   }

  }


  
public static int sqlMaxValue(string sTable, string sField)

  
{

   
string ssql = "select max(" + sField + ") as MAXVALUE from " + sTable;

   
try

   
{

    
if (conn.State == ConnectionState.Closed)

    
@H_502_1676@{

     conn.open();

    }

    sqlCeDataAdapter adp 
= new sqlCeDataAdapter(ssql, conn);

    DataSet ds 
= new DataSet();

    adp.Fill(ds);

    
if (ds.Tables.Count > 0)

    
{

     
if( ds.Tables[0].Rows.Count > 0)

     
{

      
return Common.StrToIntDef(ds.Tables[0].Rows[0][0].ToString(), 0);

     }

    }

   }

   
catch

   
{

    MessageBox.Show(
"查询失败, MessageBoxDefaultButton.Button1);

    
return -1;

   }

   
return 1;

  }


  
public static string sqlValue(string ssql)

  
{

   
try

   
{

    
if (conn.State == ConnectionState.Closed)

    
{

     conn.open();

    }

    sqlCeDataAdapter adp 
= new sqlCeDataAdapter(ssql, conn);

    DataSet ds 
= new DataSet();

    adp.Fill(ds);

    
if (ds.Tables.Count > 0)

    
{

     
if( ds.Tables[0].Rows.Count > 0)

     
{

      
return ds.Tables[0].Rows[0][0].ToString();

     }

    }

   }

   
catch

   
{

    MessageBox.Show(
"查询失败, MessageBoxDefaultButton.Button1);

    
return "";

   }

   
return "";

  }


  
public static bool sqlExec(string ssql)

  
{

   
try

   
{

    
if (conn.State == ConnectionState.Closed)

    
{

     conn.open();

    }

    cmd.CommandText 
= ssql;

    cmd.ExecuteNonQuery();

   }

   
catch

   
{

    MessageBox.Show(
"执行失败, MessageBoxDefaultButton.Button1);

    
return false;

   }

   
return true;

  }


  
public static bool sqlExec(string ssqlbool bCommit)

  
{

   
try

   
{

    
if (conn.State == ConnectionState.Closed)

    
{

     conn.open();

    }

    
if (!bCommit)

    
{

     trans 
= conn.BeginTransaction();

     cmd.Transaction 
= trans;

    }

    cmd.CommandText 
= ssql;

    cmd.ExecuteNonQuery();

    
if (bCommit)

    
{

     
if (trans != null)

     
{

      trans.Commit();

      trans 
= null;

     }

    }

   }

   
catch

   
{

    trans.Rollback();

    MessageBox.Show(
"执行失败, MessageBoxDefaultButton.Button1);

    
return false;

   }

   
return true;

  }


  
public static bool sqlExist(string ssql)

  
{

   
try

   
{

    
if (conn.State == ConnectionState.Closed)

    
{

     conn.open();

    }

    sqlCeDataAdapter adp 
= new sqlCeDataAdapter(ssql, conn);

    DataSet ds 
= new DataSet();

    adp.Fill(ds);

    
if (ds.Tables.Count > 0)

    
{

     
if( ds.Tables[0].Rows.Count > 0)

     
{

      
if (Common.StrToIntDef(ds.Tables[0].Rows[0][0].ToString(), 0> 0)

      
{

       
return true;

      }

     }

    }

   }

   
catch

   
{

    MessageBox.Show(
"查询失败, MessageBoxDefaultButton.Button1);

    
return true;

   }

   
return false;

  }


  
public static bool sqlProc(string sProcName, params ProcParam[] pProcParams)

  
{

   
try

   
{

    
if (conn.State == ConnectionState.Closed)

    
{

     conn.open();

    }

    cmd.CommandType 
= CommandType.StoredProcedure;

    cmd.CommandText 
= sProcName;

    cmd.Parameters.Clear();


    sqlCeParameter para 
= cmd.Parameters.Add("@RETURN_VALUE"sqlDbType.Int);

    para.Direction 
= ParameterDirection.ReturnValue;


    
for (int i = 0; i < pProcParams.Length; i ++)

    
{

     para 
= cmd.Parameters.Add(pProcParams[i].ParamName, pProcParams[i].ParamType);

     para.Value 
= pProcParams[i].ParamValue;

    }

    para 
= cmd.Parameters.Add("@err_msg"sqlDbType.VarChar, 80);

    para.Direction 
= ParameterDirection.Output;


    cmd.ExecuteNonQuery();

    
if (Common.StrToIntDef(cmd.Parameters["@RETURN_VALUE"].Value.ToString(), -1< 0)

    
{

     MessageBox.Show(cmd.Parameters[
"@err_msg"].Value.ToString(), MessageBoxDefaultButton.Button1);

     
return false;

    }

   }

   
catch(Exception e)

   
{

    MessageBox.Show(e.Message 
+ "调用存储过程失败", MessageBoxDefaultButton.Button1);

    
return false;

   }

   
return true;

  }


  
public static void CmbAdd(ref System.Windows.Forms.ComboBox cmb, string ssql)

  
{

   
try

   
{

    
if (conn.State == ConnectionState.Closed)

    
{

     conn.open();

    }

    sqlCeDataAdapter adp 
= new sqlCeDataAdapter(ssql, conn);

    DataSet ds 
= new DataSet();

    adp.Fill(ds);

    cmb.Items.Clear();

    
if (ds.Tables.Count > 0)

    
{

     
for (int i = 0; i < ds.Tables[0].Rows.Count; i ++)

     
{

      cmb.Items.Add(ds.Tables[
0].Rows[i][0].ToString() + "-" + ds.Tables[0].Rows[i][1].ToString());

     }

     cmb.Selectedindex 
= 0;

    }

   }

   
catch

   
{

    MessageBox.Show(
"查询失败, MessageBoxDefaultButton.Button1);

   }

  }


  
public static void CmbAdd(ref System.Windows.Forms.ComboBox cmb, string ssqlbool bBlank)

  
{

   
try

   
{

    
if (conn.State == ConnectionState.Closed)

    
{

     conn.open();

    }

    sqlCeDataAdapter adp 
= new sqlCeDataAdapter(ssql, conn);

    DataSet ds 
= new DataSet();

    adp.Fill(ds);

    cmb.Items.Clear();

    
if (ds.Tables.Count > 0)

    
{

     
if (bBlank)

     
{

      cmb.Items.Add(
"");

     }

     
for (int i = 0; i < ds.Tables[0].Rows.Count; i ++)

     
{

      cmb.Items.Add(ds.Tables[
0].Rows[i][0].ToString() + "-" + ds.Tables[0].Rows[i][1].ToString());

     }

     cmb.Selectedindex 
= 0;

    }

   }

   
catch

   
{

    MessageBox.Show(
"查询失败, MessageBoxDefaultButton.Button1);

   }

  }

 }


 
public class ProcParam

 
{

  
public string ParamName;

  
public sqlDbType ParamType;

  
public object ParamValue;


  
public ProcParam(string Name, sqlDbType Type, object Value)

  
{

   ParamName 
= Name;

   ParamType 
= Type;

   ParamValue 
= Value;

  }

 }

}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐