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

来源:互联网 发布:linux文本内容查找命令 编辑:程序博客网 时间:2024/04/29 14:12

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


原创粉丝点击