连接 SQL Server数据库并插入一条数据

时间:2015/10/17 11:07:00来源:互联网 作者:flyso 点击: 884 次

在CS文件里加入using System.Data.SqlClient;

        //www.flyso.cn
        string s1 = str1.Text.ToString();
        string s2 = str2.Text.ToString();
        string SqlConnString = "Data Source=(local);Initial Catalog=xxdb;User ID=xxuid;Password=xxpwd";
        SqlConnection SqlConn = new SqlConnection(SqlConnString);
        SqlConn.Open();//打开数据库
        SqlCommand cmd = new SqlCommand("insert into [xx_table] (s1,s2) values ('" + s1 + "','" + s2 + "')", SqlConn);
        cmd.ExecuteNonQuery();
        SqlConn.Close();//关
        Response.Write("<script>alert('添加成功!');location.href='?';</script>");


Copyright © 2005 - 2016 flyso.cn. 飞搜 版权所有 鄂ICP备11002783号-3