• C#访问数据库增删查改代码


    查询

     /// <summary>
            /// 查询
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void button1_Click(object sender, EventArgs e)
            {
                string YiGuo = "server=.;database=YiGuo;uid=sa;pwd=sasa";
                SqlConnection MyConnerction = new SqlConnection(YiGuo);//定义一个数据库连接
                SqlCommand MyCommand = new SqlCommand("select * from Users", MyConnerction);//定义一个数据库戏迷指令
                SqlDataAdapter SelectAdapter = new SqlDataAdapter();//定义一个数据适配器;
                SelectAdapter.SelectCommand = MyCommand;//定义数据适配器的操作指令
                DataSet MyDataset = new DataSet();//定义一个数据集
                MyConnerction.Open();//打开数据库连接;
                SelectAdapter.SelectCommand.ExecuteNonQuery();//执行数据库查询指令
                MyConnerction.Close();//关闭数据库
                SelectAdapter.Fill(MyDataset);//填充数据集
                DataGrid1.DataSource = MyDataset.Tables[0];
                ////DataGrid1.DataBind();//将数据表格用数据集中的数据填充
    
            }


    添加

    /// <summary>
            /// 添加
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void DataGrid1_CellContentClick(object sender, DataGridViewCellEventArgs e)
            {
    
            }
    
            private void button2_Click(object sender, EventArgs e)
            {
                string YiGuo = "server=.;database=YiGuo;uid=sa;pwd=sasa";
                SqlConnection MyConnerction = new SqlConnection(YiGuo);
                string MyInsert = ("insert into Users (6583,15565628888,www.j&q@com,女,9420)values('" + Convert.ToString(textBox1.Text) + "','" + Convert.ToString(textBox2.Text) + "','" + Convert.ToString(textBox3.Text) + "','" + Convert.ToString(textBox4.Text) + "','" + Convert.ToString(textBox5.Text) + "')");
                SqlCommand MyCommand = new SqlCommand(MyInsert, MyConnerction);
                try//处理异常
                {
                    MyConnerction.Open();
                    MyCommand.ExecuteNonQuery();
                    MyConnerction.Close();
                }
                catch(Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }

    修改更新

     /// <summary>
            /// 修改更新
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void button3_Click(object sender, EventArgs e)
            {
                string YiGuo = "server = .;database=YiGuo;uid=sa;pwd=sasa";
                SqlConnection MyConnerction = new SqlConnection(YiGuo);
                string MyUpdata = "Update Users set 操作员 ='" + textBox2.Text + "'" + "where userName = " + "'" + textBox6.Text + "'";
                SqlCommand MyCommand = new SqlCommand(MyUpdata, MyConnerction);
                try
                {
                    MyConnerction.Open();
                    MyCommand.ExecuteNonQuery();
                    MyConnerction.Close();
                    textBox6.Text = "";
                }
                catch(Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }

    删除

    /// <summary>
            /// 删除
            /// </summary>
            /// <param name="sender"></param>
            /// <param name="e"></param>
            private void button4_Click(object sender, EventArgs e)
            {
                string YiGuo = "server = .;database = YiGuo;uid = sa; pwd=sasa";
                SqlConnection MyConnection = new SqlConnection(YiGuo);
                string MyData ="Delete from Users where UserID = "+ textBox5.Text;
                SqlCommand MyCommand = new SqlCommand(MyData,MyConnection);
                try
                {
                    MyConnection.Open();
                    MyCommand.ExecuteNonQuery();
                    MyConnection.Close();
                    textBox6.Text = "";
                }
                catch(Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
  • 相关阅读:
    Cow Rectangles&Moovie Mooving
    Sound静音问题
    Spring MVC 流程图(转)
    centos6.5配置redis服务 很好用谢谢
    如何用70行Java代码实现深度神经网络算法(转)
    java中枚举(enum)小例子。之前学过枚举但是一直没用,这里有个枚举类帮你我理解下(很肤浅)
    幸福很简单(一直不知道怎么去阐述幸福,今天终于看到一个台词觉得这个阐述还行,作一个笔记)-----------穷人好像都是这么觉得的
    ExecutorService中submit和execute的区别(转)
    spring batch部分
    java 堆栈的区别(转百度)
  • 原文地址:https://www.cnblogs.com/szmh9192/p/8950087.html
Copyright © 2020-2023  润新知