我使用 3 层架构插入数据库。我不知道我应该在 .cs 文件中的按钮单击事件上写什么代码。有人可以帮我吗?
扩展名.cs
public static DbParameter CreateAndSetParameter(this DbCommand cmd,string parameterName,DbType dbtype,int size,ParameterDirection direction)
{
DbParameter p = cmd.CreateParameter();
p.ParameterName = parameterName;
p.DbType =dbtype;
p.Size = size;
p.Direction = direction;
cmd.Parameters.Add(p);
return p;
}
public static DbParameter CreateAndSetParameter(this DbCommand cmd, string parameterName, DbType dbtype, int size,object value)
{
DbParameter p = cmd.CreateParameter();
p.ParameterName = parameterName;
p.DbType = dbtype;
p.Size = size;
p.Direction = ParameterDirection.Input;
p.Value = value;
cmd.Parameters.Add(p);
return p;
}
DataGeneric.cs
public static int AddSlider(string @Imgname,string @Imgalt)
{
int rows=-1;
string Query="insert into [slider](ImageName,ImageAlt) values(@Imgname,@Imagealt)";
DbCommand cmd = DataGeneric.CreateCommand();
cmd.CommandText = Query;
cmd.CreateAndSetParameter("@Imgname",DbType.String,50,Imgname);
cmd.CreateAndSetParameter("@Imgalt", DbType.String, 50, Imgalt);
rows = DataGeneric.ExecuteNonQuery(cmd);
return rows;
}
设计:
ImageName:<asp:TextBox id="txt_name" runat="server" /><br />
ImageAlt:<asp:TextBox ID="txt_alt" runat="server" />
<asp:Button ID="btn" runat="server" onclick="btn_Click" Text="Insert" />
代码:
protected void btn_Click(object sender, EventArgs e)
{
????????????
}