1.在web.config中配置连接字符串
<appSettings>
<add key="pub" value="Data Source=(local);Initial Catalog=BookSample;User ID=sa;Password=123"/>
</appSettings>
注意Initial Catalog是数据库名 User ID是用户名 Password密码
2.执行修改操作
protected void Button1_Click(object sender, EventArgs e)
{
string constr = ConfigurationManager.ConnectionStrings["pub"].ToString();
SqlConnection myconn = new SqlConnection(constr);
myconn.Open();
string mysql = "insert into students values('***','123456','****','****','****')";
SqlCommand mycommand = new SqlCommand(mysql,myconn);
int i = mycommand.ExecuteNonQuery();
Response.Write(i.ToString()+"行受影响");
myconn.Close();
}
3.修改和删除只是sql语句不一样
4. 读取数据到listbox
protected void Button2_Click(object sender, EventArgs e)
{

本文介绍了.NET中如何在Web应用程序中连接到数据库并进行数据操作,包括在web.config中配置连接字符串,执行插入、查询、删除和更新操作。示例代码展示了使用SqlConnection和SqlCommand进行数据读写,以及将数据绑定到ListBox和GridView展示。
最低0.47元/天 解锁文章
1933

被折叠的 条评论
为什么被折叠?



