簡體   English   中英

插入SQL Server數據庫ASP.net

[英]Inserting into SQL Server database ASP.net

我想做的是在SQL Server數據庫中插入用戶名及其每月小時數限制。 我使用自動生成的語句進行更新和刪除。 我現在只需要添加新用戶。 據我所知,下面的代碼應該可以工作,但事實並非如此。 我認為這是我編寫的方式。

注釋中的部分是Userdata.aspx文件自動生成的內容,因此我嘗試將其轉換為使用2個文本框。

非常感謝。

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();
}

您需要配置數據源以使用參數。

 <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>

更新:我忘了提及,您想使用ControlParameter而不是簡單的Parameter。 看一下以下代碼片段:

  <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>

看一下相應的MSDN頁面,該頁面詳細描述了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.InsertCommand is a property.
Datasource.Insert() is a method.

您還應該使用參數。

datasource.insertparameters("username").defaultvalue = TextBox1.Text + "," + TextBox2.Text

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM