WINCE中DataGrid對(duì)SQLCE數(shù)據(jù)庫的操作
?private SqlCeConnection OpenSQLCE()
? ? ? ? {
? ? ? ? ? ? //存取數(shù)據(jù)庫
? ? ? ? ? ? //連接sql server Mobile數(shù)據(jù)庫的連接字符串
? ? ? ? ? ? string connString = "Data Source='singleinfo.sdf';Password=";
? ? ? ? ? ? SqlCeEngine engine = new SqlCeEngine(connString);
? ? ? ? ? ? //建立鏈接
? ? ? ? ? ? SqlCeConnection ConnMobile = new SqlCeConnection(connString);
? ? ? ? ? ? try
? ? ? ? ? ? {
? ? ? ? ? ? ? ? ConnMobile.Open();
? ? ? ? ? ? }
? ? ? ? ? ? catch (Exception ex)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? MessageBox.Show("不能正確連接數(shù)據(jù)庫!" + ex.Message);
? ? ? ? ? ? ? ? this.Close();
? ? ? ? ? ? ? ? throw;
? ? ? ? ? ? }
? ? ? ? ? ? return ConnMobile;
? ? ? ? }
//**********************************************
?//統(tǒng)計(jì)表中的數(shù)據(jù)元組總數(shù)
? ? ? ? ? ? string Strnum = "select count(*) from SingleDatas where Commandeco is not null";
? ? ? ? ? ? SqlCeCommand CommandNum = new SqlCeCommand(Strnum, ConnMobile);
? ? ? ? ? ? string TotalLine = CommandNum.ExecuteScalar().ToString();
? ? ? ? ? ? labelNum.Text = Convert.ToString(TotalLine); ? ?// 顯示數(shù)據(jù)記錄總數(shù) ? ? ?
? ? ? ? ? ? int b = Convert.ToInt32(TotalLine);
? ? ? ? ? ? if (0 == b)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? tableRow = b + 1; ? ? ? ? ? ? ? ?
? ? ? ? ? ? }
? ? ? ? ? ? else
? ? ? ? ? ? {
? ? ? ? ? ? ? ? //獲取最大的Commandeco值
? ? ? ? ? ? ? ? string strcmd = "Select max(Commandeco) from SingleDatas";
? ? ? ? ? ? ? ? SqlCeCommand commd = new SqlCeCommand(strcmd, ConnMobile);
? ? ? ? ? ? ? ? string A = commd.ExecuteScalar().ToString();
? ? ? ? ? ? ? ? tableRow = Convert.ToInt32(A) + 1; ? ? ? ? ? ? ??
? ? ? ? ? ? }
? ? ? ? ? ? //MessageBox.Show("插入第一條數(shù)據(jù):" + tableRow.ToString()); ?
? ? ? ? ? ? string sql = "Insert into SingleDatas (Commandeco,Tosend,Received) values (" + tableRow + ",'" + Convert.ToString(sendmsg.Text) + "','" + Convert.ToString(recmsg.Text) + "')";
? ? ? ? ? ? SqlCeCommand command = new SqlCeCommand(sql, ConnMobile);
? ? ? ? ? ?
? ? ? ? ? ? int r = command.ExecuteNonQuery(); ?//插入數(shù)據(jù)影響行 ? ?
? ? ? ? ? ? if (r > 0)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? MessageBox.Show("添加數(shù)據(jù)成功!");
? ? ? ? ? ? } ?
? ? ? ? ? ??
///******************************************************
?//在DataGrid1顯示數(shù)據(jù)
? ? ? ? ? ? string str = "select * from SingleDatas";
? ? ? ? ? ? DataSet dataset = new DataSet();
? ? ? ? ? ? SqlCeDataAdapter adapter = new SqlCeDataAdapter(str, ConnMobile);
? ? ? ? ? ? try
? ? ? ? ? ? {
? ? ? ? ? ? ? ? adapter.Fill(dataset, "SingleDatas");
? ? ? ? ? ? ? ? dataGrid1.DataSource = dataset.Tables["SingleDatas"];
? ? ? ? ? ? }
? ? ? ? ? ? catch (Exception ex)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? MessageBox.Show("出錯(cuò)信息" + ex.Message);
? ? ? ? ? ? ? ? this.Close();
? ? ? ? ? ? ? ? throw;
? ? ? ? ? ? }
? ? ? ? ? ? ConnMobile.Close();
//****************************************************
?SqlCeConnection connMbile = OpenSQLCE();
? ? ? ? ? ? try
? ? ? ? ? ? {
? ? ? ? ? ? ? ? //獲取當(dāng)前選中行的ID值。?
? ? ? ? ? ? ? ? string myID = this.dataGrid1[dataGrid1.CurrentCell.RowNumber, 2].ToString().Trim();
? ? ? ? ? ? ? ? MessageBox.Show("你將刪除第" + myID + "元組");
? ? ? ? ? ? ? ? // string MyDelete = "Delete from SingleDatas where Commandeco=" + dataGrid1.CurrentCell.RowNumber; ? //刪除當(dāng)前選中的行數(shù)的
? ? ? ? ? ? ? ? string MyDelete = "Delete from SingleDatas where Commandeco=" + Convert.ToInt32(myID);
? ? ? ? ? ? ? ? SqlCeCommand MyCommand = new SqlCeCommand(MyDelete, connMbile);
? ? ? ? ? ? ? ? MyCommand.ExecuteNonQuery();
? ? ? ? ? ? }
? ? ? ? ? ? catch (Exception ex)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? Console.WriteLine("{0} Exception caught.", ex);
? ? ? ? ? ? }