[英]Inserting into SQL Server database ASP.net
What I'm trying to do is to insert a username, and their monthly hour limit to my SQL Server database. 我想做的是在SQL Server数据库中插入用户名及其每月小时数限制。 I've used the automatically generated statements for updating and deleting.
我使用自动生成的语句进行更新和删除。 I just need to add in new users now.
我现在只需要添加新用户。 The code below, should work as far as I know, but it doesn't.
据我所知,下面的代码应该可以工作,但事实并非如此。 I think it's the way I've written it.
我认为这是我编写的方式。
The part in comments is what the Userdata.aspx
file automatically generated, so I'm trying to convert it to use my 2 text boxes. 注释中的部分是
Userdata.aspx
文件自动生成的内容,因此我尝试将其转换为使用2个文本框。
Thanks a lot. 非常感谢。
protected void Button1_Click1(object sender, EventArgs e)
{
string sql = "INSERT INTO [UserData]([UserName], [MonthlyHourLimit]) VALUES ("+ TextBox1.Text + "," + TextBox2.Text + ")";
//INSERT INTO [UserData] ([UserName], [MonthlyHourLimit]) VALUES (@UserName, @MonthlyHourLimit)"
SqlDataSource1.InsertCommand = sql;
GridView1.DataBind();
}
You need to configure your data source to use parameters. 您需要配置数据源以使用参数。
<asp:sqlDataSource ID="EmployeeDetailsSqlDataSource"
SelectCommand="select [UserName], [MonthlyHourLimit] from [UserData] where UserName= @UserName"
InsertCommand="IINSERT INTO [UserData] ([UserName], [MonthlyHourLimit]) VALUES (@UserName, @MonthlyHourLimit);"
ConnectionString="<%$ ConnectionStrings:MyConnection %>"
RunAt="server">
<SelectParameters>
<asp:Parameter Name="UserName" Type="Int32" DefaultValue="0" />
</SelectParameters>
<InsertParameters>
<asp:Parameter Name="UserName" Direction="Input" Type="String" />
<asp:Parameter Name="MonthlyHourLimit" Direction="Input" Type="String" />
</InsertParameters>
</asp:sqlDataSource>
UPDATE:I've forgot to mention, you would like to use ControlParameter and not simple Parameter. 更新:我忘了提及,您想使用ControlParameter而不是简单的Parameter。 Take a look at following snippet:
看一下以下代码片段:
<asp:СontrolParameter Name="UserName" ControlId="ddlUserNames" PropertyName="SelectedValue"/>
...
<asp:DropdownList
ID="ddlUserNames"
runat="server"
Autopostback="True">
<asp:Listitem Selected="True">Users</asp:Listitem>
<asp:Listitem Value="Peter">Peter</asp:Listitem>
<asp:Listitem Value="Jessica">Jessica</asp:Listitem>
</asp:Dropdownlist>
Take a look at corresponding MSDN page describing usage of SqlDataSource in details. 看一下相应的MSDN页面,该页面详细描述了SqlDataSource的用法。
UPDATED 2: complete example in order to avoid confusion 更新2:完整示例,以避免混淆
<asp:sqlDataSource ID="EmployeeDetailsSqlDataSource"
SelectCommand="select [UserName], [MonthlyHourLimit] from [UserData] where UserName= @UserName"
InsertCommand="IINSERT INTO [UserData] ([UserName], [MonthlyHourLimit]) VALUES (@UserName, @MonthlyHourLimit);"
ConnectionString="<%$ ConnectionStrings:MyConnection %>"
RunAt="server">
<SelectParameters>
<asp:Parameter Name="UserName" Type="Int32" DefaultValue="0" />
</SelectParameters>
<InsertParameters>
<asp:ControlParameter Name="UserName" ControlId="txtUserName" Direction="Input" Type="String" />
<asp:ControlParameter Name="MonthlyHourLimit" ControlId="txtMonthlyHourLimit" Direction="Input" Type="String" />
</InsertParameters>
</asp:sqlDataSource>
<asp:TextBox runat="server" ID="txtUserName" />
<asp:TextBox runat="server" ID="txtMonthlyHourLimit" />
Datasource.InsertCommand is a property.
Datasource.Insert() is a method.
You should also use parameters. 您还应该使用参数。
datasource.insertparameters("username").defaultvalue = TextBox1.Text + "," + TextBox2.Text
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.