C#连接数据库Sql 2008实现增删改查

C#连接数据库Sql 2008实现增删改查_第1张图片
C#连接数据库Sql 2008实现增删改查_第2张图片
实现数据库增加数据:

            string MyConn = "Server=localhost;initial catalog=Library;user id=sa;password=236;Connect Timeout=5";    // localhost:本地数据库,Library:数据库名,sa:登录名,236:密码,5:超时时间
            SqlConnection MyConnection = new SqlConnection(MyConn);
            string MyInsert = "insert into reader(cardid,cardname,Timer) values('" + Convert.ToString(tbreaderid.Text) + "','" + Convert.ToString(tbreadername.Text) + "','" + DateTime.Now + "')";
            SqlCommand MyCommand = new SqlCommand(MyInsert, MyConnection);
            try           //   异常处理
            {
                MyConnection.Open();                      // 打开数据库
                MyCommand.ExecuteNonQuery();              // 执行sql语句
                MyConnection.Close();                     // 关闭数据库
                tbreaderid.Text = "";
                tbreadername.Text = "";
                MessageBox.Show("添加成功", "提示");
            }
            catch
            {
                MessageBox.Show("添加失败", "提示");
            }

实现数据库删除数据:

            string MyConn = "Server=localhost;initial catalog=Library;user id=sa;password=236;Connect Timeout=5";
            SqlConnection MyConnection = new SqlConnection(MyConn);
            string MyDelete = "Delete from reader where cardid = '" + tbreaderid.Text + "' ";
            SqlCommand MyCommand = new SqlCommand(MyDelete, MyConnection);
            try
            {
                MyConnection.Open();
                MyCommand.ExecuteNonQuery();
                MyConnection.Close();
                tbreaderid.Text = "";
                tbreadername.Text = "";
                MessageBox.Show("删除成功", "提示");
            }
            catch
            {
                MessageBox.Show("删除失败","提示");
            }

实现数据库修改数据:

	        string MyConn = "Server=localhost;initial catalog=Library;user id=sa;password=236;Connect Timeout=5";
            SqlConnection MyConnection = new SqlConnection(MyConn);
            string MyDelete = "UPDATE  reader SET cardname = ‘ ” + tbreadername.Text + “’ ” + “where cardid = '" + tbreaderid.Text + "' ";
            SqlCommand MyCommand = new SqlCommand(MyDelete, MyConnection);
            try
            {
                MyConnection.Open();
                MyCommand.ExecuteNonQuery();
                MyConnection.Close();
                tbreaderid.Text = "";
                tbreadername.Text = "";
                MessageBox.Show("修改成功", "提示");
            }
            catch
            {
                MessageBox.Show("修改失败","提示");
            }

实现数据库查询数据:

            string MyConnection= "Server=localhost;initial catalog=Library;user id=sa;password=236;Connect Timeout=5";
            string Mymand = "SELECT cardname FROM reader WHERE cardid = '" + tbbookid.Text + "' ";
            SqlCommand MyCommand2 = new SqlCommand(Mymand, MyConnection);   // 定义一个数据库操作指令
            SqlDataAdapter SelectAdapter2 = new SqlDataAdapter();           // 定义一个数据适配器
            SelectAdapter2.SelectCommand = MyCommand2;                      // 定义数据适配器的操作指令
            DataSet MyDataSet2 = new DataSet();                             // 定义一个数据集
            try
            {
                MyConnection.Open();                                        // 打开数据库连接
                SelectAdapter2.SelectCommand.ExecuteNonQuery();             // 执行数据库查询指令
                MyConnection.Close();                                       // 关闭数据库
                SelectAdapter2.Fill(MyDataSet2);                            // 填充数据集
                DataTable dd = MyDataSet2.Tables[0];
                DataRow ddd = dd.Rows[0];
                tbbookname.Text = ("" + ddd[0]).Trim();                     // 将查到的数据赋值到文本框中
                tbbookid.Text = "";
                tbbookname.Text = "";
            }
            catch
            {
                MessageBox.Show("查询失败",“提示”);
            }

你可能感兴趣的:(备忘录)