其实就是写一个连接类,然后调用就可以了;
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;
///
/// DBConnection 的摘要说明
///
public class DBconnection
{
SqlConnection conn = null;
SqlCommand cmd = new SqlCommand();
public DBconnection()
{
conn = new SqlConnection("server=.;database=university_db;uid=sa;pwd=123;");
}
public SqlConnection getconn()
{
if (conn.State == ConnectionState.Closed)
conn.Open();//打开连接
return conn;
}
//关闭连接
public void clear()
{
if (conn.State == ConnectionState.Open)
conn.Close();
}
}
然后在你要用连接的页面的.cs页面调用就可以了呀
上面的问题就是你的答案,太巧了
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
string s = System.Configuration.ConfigurationManager.ConnectionStrings["loginConnectionString"].ToString();
SqlConnection con = new SqlConnection(s);
con.Open();
string name = this.TextBox1.Text.Trim();
string pwd = this.TextBox2.Text.Trim();
string sql = "insert into denglu(name,pwd)values('"+name+"','"+pwd+"')";
SqlCommand cmd = new SqlCommand(sql,con);
cmd.ExecuteNonQuery();
Response.Write("");
从问题可以看出你是刚入门的
花些时间看入门教程去