数据库的连接

来源:互联网 发布:linux mint 18界面美化 编辑:程序博客网 时间:2024/06/05 20:27

 

 今天学习了数据库的连接:

首先弄个字符串连接string strCnn = "server=.;database=students;integrated security=true";

然后在创建一个连接SqlConnection sqlCnn = new SqlConnection();
        sqlCnn.ConnectionString = strCnn;

之后在执行命令:SqlCommand sqlCmm = new SqlCommand();

sqlCmm.CommandText = string.Format("insert into student(sname,scity)values('{0}','{1}')", this.TextBox1.Text, this.TextBox2.Text);对数据库进行操作
        sqlCmm.Connection = sqlCnn;

         

        sqlCnn.Open();
        int rows = sqlCmm.ExecuteNonQuery();  

        sqlCnn.Close();

原创粉丝点击