WINCE中DataGrid对SQLCE数据库的打操作(C#)

 private SqlCeConnection OpenSQLCE()
        {
            //存取数据库
            //连接sql server Mobile数据库的连接字符串
            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("不能正确连接数据库!" + ex.Message);
                this.Close();
                throw;
            }
            return ConnMobile;

        }



//**********************************************

 //统计表中的数据元组总数
            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);    // 显示数据记录总数      
            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("插入第一条数据:" + 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();  //插入数据影响行    
            if (r > 0)
            {
                MessageBox.Show("添加数据成功!");
            }  
            

///******************************************************

 //在DataGrid1显示数据
            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("出错信息" + ex.Message);
                this.Close();
                throw;
            }
            ConnMobile.Close();


//****************************************************

 SqlCeConnection connMbile = OpenSQLCE();
            try
            {


                //获取当前选中行的ID值。 
                string myID = this.dataGrid1[dataGrid1.CurrentCell.RowNumber, 2].ToString().Trim();

                MessageBox.Show("你将删除第" + myID + "元组");
                // string MyDelete = "Delete from SingleDatas where Commandeco=" + dataGrid1.CurrentCell.RowNumber;   //删除当前选中的行数的

                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);
            }


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值