C# 在SQL Server数据库ASP.net中插入

C# 在SQL Server数据库ASP.net中插入,c#,asp.net,sql-server,C#,Asp.net,Sql Server,我想做的是在我的SQL Server数据库中插入一个用户名和他们的每月工作时间限制。我已经使用自动生成的语句进行更新和删除。我现在只需要添加新用户。就我所知,下面的代码应该可以工作,但它不能。我想这是我写的方式 comments中的部分是Userdata.aspx文件自动生成的内容,因此我尝试将其转换为使用我的两个文本框 非常感谢 protected void Button1_Click1(object sender, EventArgs e) { string sql = "INSE

我想做的是在我的SQL Server数据库中插入一个用户名和他们的每月工作时间限制。我已经使用自动生成的语句进行更新和删除。我现在只需要添加新用户。就我所知,下面的代码应该可以工作,但它不能。我想这是我写的方式

comments中的部分是
Userdata.aspx
文件自动生成的内容,因此我尝试将其转换为使用我的两个文本框

非常感谢

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();
}
您还应该使用参数

datasource.insertparameters("username").defaultvalue = TextBox1.Text + "," + TextBox2.Text
 <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>
您还应该使用参数

datasource.insertparameters("username").defaultvalue = TextBox1.Text + "," + TextBox2.Text
 <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>

您需要配置数据源以使用参数

datasource.insertparameters("username").defaultvalue = TextBox1.Text + "," + TextBox2.Text
 <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>
而不是简单的参数。请看以下代码段:

  <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>
详细描述SqlDataSource用法的页面

更新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.insertparameters("username").defaultvalue = TextBox1.Text + "," + TextBox2.Text
 <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>
而不是简单的参数。请看以下代码段:

  <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>
详细描述SqlDataSource用法的页面

更新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" />



这有什么不起作用?您是否遇到错误?首先要注意的是SQL注入。最好创建一个SqlCommand类isntance,其中包含从用户处读取的每个值的参数。虽然这可能不是您当前的问题,但我没有收到错误,只是单击我的按钮时什么也没有发生。生成的代码比您的代码好得多-它使用参数避免将SQL语句串联在一起,从而打开SQL注入攻击的大门-只是不要这样做从来没有。但是生成的代码对我没有帮助,因为我无法从框中获取数据并在button1\u click的事件中插入。这有什么不起作用?您是否遇到错误?首先要注意的是SQL注入。最好创建一个SqlCommand类isntance,其中包含从用户处读取的每个值的参数。虽然这可能不是您当前的问题,但我没有收到错误,只是单击我的按钮时什么也没有发生。生成的代码比您的代码好得多-它使用参数避免将SQL语句串联在一起,从而打开SQL注入攻击的大门-只是不要这样做从来没有。但是生成的代码对我没有帮助,因为我无法从框中获取数据并在按钮1上插入。\u click的事件。\Vittore,这种方法比尝试构建干净/安全的动态SQL语句要好得多,也更安全。@tgolisch相信我,我知道!因此,如果要使用aspx中的参数。然后如何定义它们并在.cs文件中使用它们?可以定义ControlParameter,这样就不需要代码,也可以定义general parameter,这样就可以从代码后面进行设置。我在回答中给出的第二个链接中有详细的解释。最有可能的是,在这种情况下,您不需要在代码隐藏中对其进行更改,而是使用ControlParameter、SessionParameter等的组合。抱歉,我仍然不明白如何通过单击我的按钮#Vittore将这些框中的内容输入数据库,这种方法比试图构建干净/安全的动态SQL语句要好得多,也更安全。@tgolisch相信我,我知道!因此,如果要使用aspx中的参数。然后如何定义它们并在.cs文件中使用它们?可以定义ControlParameter,这样就不需要代码,也可以定义general parameter,这样就可以从代码后面进行设置。我在回答中给出的第二个链接中有详细的解释。最有可能的情况是,在这种情况下,您不需要在代码隐藏中更改它,而是使用ControlParameter、SessionParameter等的组合。对不起,我仍然不明白如何通过单击我的按钮将这些框上的内容输入数据库,但是insertparameters也不是一个方法?但是insertparameters也不是一个方法?