使用SqlDataSource控件和GridView控件完成数据的选择、添加、修改和删除功能;
1个回答
展开全部
<connectionStrings>
<add name="ConnStr" connectionString="Data Source=.;Initial Catalog=Northwind;Integrated Security=True"/>
</connectionStrings>
后台
protected void Page_Load(object sender, EventArgs e)
{
bind();
}
void bind()
{
using (SqlConnection conn = new SqlConnection())
{
conn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;
SqlDataAdapter da = new SqlDataAdapter("select * from test", conn);
DataSet ds = new DataSet();
da.Fill(ds, "test");
GridView1.DataSource = ds;
GridView1.DataBind();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
using (SqlConnection conn = new SqlConnection())
{
conn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;
SqlDataAdapter da = new SqlDataAdapter("select * from test", conn);
SqlCommandBuilder scb = new SqlCommandBuilder(da);
DataSet ds = new DataSet();
da.Fill(ds, "test");
DataRow dr = ds.Tables[0].NewRow();
dr["name"] = TextBox1.Text;
ds.Tables[0].Rows.Add(dr);
da.Update(ds, "test");
bind();
}
}
<add name="ConnStr" connectionString="Data Source=.;Initial Catalog=Northwind;Integrated Security=True"/>
</connectionStrings>
后台
protected void Page_Load(object sender, EventArgs e)
{
bind();
}
void bind()
{
using (SqlConnection conn = new SqlConnection())
{
conn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;
SqlDataAdapter da = new SqlDataAdapter("select * from test", conn);
DataSet ds = new DataSet();
da.Fill(ds, "test");
GridView1.DataSource = ds;
GridView1.DataBind();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
using (SqlConnection conn = new SqlConnection())
{
conn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnStr"].ConnectionString;
SqlDataAdapter da = new SqlDataAdapter("select * from test", conn);
SqlCommandBuilder scb = new SqlCommandBuilder(da);
DataSet ds = new DataSet();
da.Fill(ds, "test");
DataRow dr = ds.Tables[0].NewRow();
dr["name"] = TextBox1.Text;
ds.Tables[0].Rows.Add(dr);
da.Update(ds, "test");
bind();
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询