通过SQL数据库账号连接
string conString = "server=(local);uid=sa;pwd=sa;database=Student";
string conString ="Data Source=(local);uid=sa;pwd=sa;database=Student";
通过Windows身份认证连接
string conString ="Data Source=.;Initial Catalog=dataset;Integrated Security=True":
// 添加
SqlConnection conn = new SqlConnection(conString);
SqlCommand cmd = new SqlCommand("insert into S_Province values('"+a+"','"+b+"'),conn);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
//删除
string conString = "server=(local);uid=sa;pwd=sa;database=Student";
SqlConnection conn = new SqlConnection(conString);
SqlCommand cmd = new SqlCommand("delete from S_Province,conn);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
//修改
string conString = ConfigurationManager.ConnectionStrings["conString"].ConnectionString;
SqlConnection conn = new SqlConnection(conString);
SqlCommand cmd = new SqlCommand("update S_Province",conn);
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
//查
string conString = "server=(local);uid=sa;pwd=sa;database=Student";
SqlConnection conn = new SqlConnection(conString);
SqlDataAdapter sda = new SqlDataAdapter("SELECT * FROM S_Province", conn);
DataSet ds = new DataSet();
sda.Fill(ds);
//Web.config
<connectionStrings>
<add name="conString" connectionString="server=CP79S0F\SQLEXPRESS;database=News;uid=sa;pwd=sa;"/>
</connectionStrings>
<appSettings>
<add key="conString" value="server=CP79S0F\SQLEXPRESS;database=News;uid=sa;pwd=sa;"/>
</appSettings>