您当前的位置: 首页 >  c#

衣舞晨风

暂无认证

  • 3浏览

    0关注

    1156博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

C# Sql Server连接(增、删、改、查)

衣舞晨风 发布时间:2014-01-08 09:36:36 ,浏览量:3

        //增加、删除、更新、查询公用       

string MyConn = "server=127.0.0.1;uid=数据库登录名;pwd=密码;database=数据库的名字;Trusted_Connection=no";
SqlConnection MyConnection = new SqlConnection(MyConn);

1、增加
          string MyInsert = "想要执行的sql语句";
          SqlCommand MyCommand = new SqlCommand(MyInsert, MyConnection);
          MyConnection.Open();
          MyCommand.ExecuteNonQuery();
          MyConnection.Close();

2、删除

           string MyDelete = "想要执行的sql语句";
            SqlCommand MyCommand = new SqlCommand(MyDelete, MyConnection);
            MyConnection.Open();
            MyCommand.ExecuteNonQuery();
            MyConnection.Close();

3、更新

           string MyUpdate = "想要执行的sql语句";
            SqlCommand MyCommand = new SqlCommand(MyUpdate, MyConnection);
            MyConnection.Open();
            MyCommand.ExecuteNonQuery();
             MyConnection.Close();

4、查询

            MyConn.Open();
            SqlDataAdapter dap = new SqlDataAdapter("想要执行的sql语句",MyConn);
            DataSet ds = new DataSet();//实例化DataSet类
            dap.Fill(ds, "Table");//添加SQL语句并执行
            dataGridView1.DataSource = ds.Tables[0].DefaultView;//显示数据

      

关注
打赏
1647422595
查看更多评论
立即登录/注册

微信扫码登录

0.0395s