现在的位置: 首页 > 综合 > 正文

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

2013年11月03日 ⁄ 综合 ⁄ 共 3298字 ⁄ 字号 评论关闭

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(
"连接数据库失败""提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
    
return false;
   }

   
return true;
  }


  
public static bool Close()
  
{
   
try
   
{
    conn.Close();
    
return true;
   }

   
catch
   
{
    
return false;
   }

  }


  
public static bool SQLQuery(string sSQL, ref 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 + " ]""提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, MessageBoxDefaultButton.Button1);
    
return false;
   }

   
return true;
  }

  
  
public static bool SQLQuery(string sSQL, ref 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(
"查询失败, SQL[ " + sSQL + " ]""提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation, 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)
    

抱歉!评论已关闭.