新建一个表,设计数据表如下:
然后,我们打开新建查询窗口,写存储过程代码:
use[Study]
GO
Create Procedure Language
@ID int
AS
if Exists(select * from Table_6 where Number >0)
begin
update Table_6 set Number=(Number+1) where ID=@ID
end
else
update Table_6 set Number=1 where ID=@ID
GO
写完代码后,点执行存储过程,看看有没有错。而后,我们在VS中新建一个web页面,而后在设计.aspx页面:
而后,我们在.cs文件写C#代码:
SqlConnection conn;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
DVBind();
}
}
protected void DVBind()
{
string strconn = "server=.;database=Study;user id=sa;password=120110123456";
conn = new SqlConnection(strconn);
conn.Open();
string sqlStr = "select * from Table_6";
SqlDataAdapter da = new SqlDataAdapter(sqlStr, conn);
DataSet ds = new DataSet();
da.Fill(ds);
conn.Close();
GridView1.DataSource = ds;
GridView1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
string strconn = "server=.;database=Study;user id=sa;password=123456789";
conn = new SqlConnection(strconn);
conn.Open();
Int32 selectIndex = Convert.ToInt32(RadioButtonList1.SelectedIndex)+1;
SqlCommand cmd = new SqlCommand("Language", conn);
cmd.CommandType=CommandType.StoredProcedure;
SqlParameter voteID = new SqlParameter("@ID", SqlDbType.Int, 4);
voteID.Value = selectIndex;
cmd.Parameters.Add(voteID);
cmd.ExecuteNonQuery();
cmd.Dispose();
conn.Close();
}
最后实现结果:
图4的话需要我们刷新之后才能显示出来