INSERT存储过程可以工作,但从方法调用时未插入数据吗?

时间:2019-03-07 22:08:45

标签: c# asp.net sql-server stored-procedures

我正在尝试将数据插入表中。我创建了一个存储过程将执行此操作。我还创建了一个调用存储过程的方法。当我单击提交按钮时,将触发click事件,并获取输入数据并调用该方法以插入到表中。

我没有收到任何错误,但是当我检入数据库时​​,数据尚未插入。

我尝试手动执行存储过程,它确实起作用。

这是我的存储过程:

CREATE PROCEDURE [dbo].AddRestaurant
    @theName VARCHAR(250),
    @theAddr VARCHAR(350), 
    @theCity VARCHAR(50),
    @theState VARCHAR(2),
    @theZip VARCHAR(5),
    @theCategory INT
AS
BEGIN
    INSERT INTO Restaurants (RestName, RestAddr, RestCity, RestState, RestZip, CategoryID)
    VALUES (@theName, @theAddr, @theCity, @theState, @theZip, @theCategory)
END

这是我的表结构:

CREATE TABLE [dbo].[Restaurants] 
(
    [RestaurantID] INT IDENTITY (1, 1) NOT NULL,
    [RestName]     VARCHAR(250) NULL,
    [RestAddr]     VARCHAR(350) NULL,
    [RestCity]     VARCHAR(50)  NULL,
    [RestState]    VARCHAR(2)   NULL,
    [RestZip]      VARCHAR(5)   NULL,
    [CategoryID]   INT          NULL,

    PRIMARY KEY CLUSTERED ([RestaurantID] ASC),

    CONSTRAINT [FK_Restaurants_CatergoryID] 
        FOREIGN KEY ([CategoryID]) 
        REFERENCES [dbo].[Categories] ([CategoryID])
);

这是我的方法:

public void AddRestaurant(string name, string addr, string city, string state, string zip, int category)
{
    DBConnect objDB = new DBConnect();

    objCmd.Parameters.Clear();
    objCmd.CommandType = CommandType.StoredProcedure;
    objCmd.CommandText = "AddRestaurant";

    objCmd.Parameters.AddWithValue("@theName", name);
    objCmd.Parameters.AddWithValue("@theAddr", addr);
    objCmd.Parameters.AddWithValue("@theCity", city);
    objCmd.Parameters.AddWithValue("@theState", state);
    objCmd.Parameters.AddWithValue("@theZip", zip);
    objCmd.Parameters.AddWithValue("@theCategory", category);

    objDB.GetConnection().Open();
    objDB.DoUpdateUsingCmdObj(objCmd);
    int affectedRows = objDB.DoUpdateUsingCmdObj(objCmd);
    objDB.CloseConnection();
}

这是aspx.cs文件中的click事件:

protected void btnSubmit_Click(object sender, EventArgs e)
{
    try
    {
        rest.RestName = txtRestName.Text;
        rest.RestAddr = txtRestAddr.Text;
        rest.RestCity = txtRestCity.Text;
        rest.RestState = ddStates.SelectedValue;
        rest.RestZip = txtRestZip.Text;
        rest.CategoryID = int.Parse(ddCategories.SelectedValue);

        p.AddRestaurant(txtRestName.Text, txtRestAddr.Text, txtRestCity.Text, ddStates.SelectedItem.Value, txtRestZip.Text, int.Parse(ddCategories.SelectedValue));

        string error = txtRestName.Text + " has been added!";
        ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + error + "');", true);
    }
    catch (SqlException ex)
    {
        string error = txtRestName.Text + " has NOT been added!" + ex;
        ClientScript.RegisterStartupScript(this.GetType(), "myalert", "alert('" + error + "');", true);
    }
}

这是我的aspx页面:

<fieldset class="center, content2">
            <legend>Restaurant Info</legend>
            <asp:Label ID="lblRestName" runat="server" CssClass="left">Name: </asp:Label>
            <asp:TextBox ID="txtRestName" runat="server" CssClass="right"></asp:TextBox>
            <br /><br />
            <asp:Label ID="lblRestAddr" runat="server" CssClass="left">Address: </asp:Label>
            <asp:TextBox ID="txtRestAddr" runat="server" CssClass="right"></asp:TextBox>
            <br /><br />
            <asp:Label ID="lblRestCity" runat="server" CssClass="left">City: </asp:Label>
            <asp:TextBox ID="txtRestCity" runat="server" CssClass="right"></asp:TextBox>
            <br /><br />
            <asp:Label ID="lblRestState" runat="server" CssClass="left">State: </asp:Label>
            <asp:DropDownList ID="ddStates" runat="server" CssClass="right" Width="140px">
                <asp:ListItem Value="AL">Alabama</asp:ListItem>
                <asp:ListItem Value="AK">Alaska</asp:ListItem>
                <asp:ListItem Value="AZ">Arizona</asp:ListItem>
                <asp:ListItem Value="AR">Arkansas</asp:ListItem>
                <asp:ListItem Value="CA">California</asp:ListItem>
                <asp:ListItem Value="CO">Colorado</asp:ListItem>
                <asp:ListItem Value="CT">Connecticut</asp:ListItem>
                <asp:ListItem Value="DC">District of Columbia</asp:ListItem>
                <asp:ListItem Value="DE">Delaware</asp:ListItem>
                <asp:ListItem Value="FL">Florida</asp:ListItem>
                <asp:ListItem Value="GA">Georgia</asp:ListItem>
                <asp:ListItem Value="HI">Hawaii</asp:ListItem>
                <asp:ListItem Value="ID">Idaho</asp:ListItem>
                <asp:ListItem Value="IL">Illinois</asp:ListItem>
                <asp:ListItem Value="IN">Indiana</asp:ListItem>
                <asp:ListItem Value="IA">Iowa</asp:ListItem>
                <asp:ListItem Value="KS">Kansas</asp:ListItem>
                <asp:ListItem Value="KY">Kentucky</asp:ListItem>
                <asp:ListItem Value="LA">Louisiana</asp:ListItem>
                <asp:ListItem Value="ME">Maine</asp:ListItem>
                <asp:ListItem Value="MD">Maryland</asp:ListItem>
                <asp:ListItem Value="MA">Massachusetts</asp:ListItem>
                <asp:ListItem Value="MI">Michigan</asp:ListItem>
                <asp:ListItem Value="MN">Minnesota</asp:ListItem>
                <asp:ListItem Value="MS">Mississippi</asp:ListItem>
                <asp:ListItem Value="MO">Missouri</asp:ListItem>
                <asp:ListItem Value="MT">Montana</asp:ListItem>
                <asp:ListItem Value="NE">Nebraska</asp:ListItem>
                <asp:ListItem Value="NV">Nevada</asp:ListItem>
                <asp:ListItem Value="NH">New Hampshire</asp:ListItem>
                <asp:ListItem Value="NJ">New Jersey</asp:ListItem>
                <asp:ListItem Value="NM">New Mexico</asp:ListItem>
                <asp:ListItem Value="NY">New York</asp:ListItem>
                <asp:ListItem Value="NC">North Carolina</asp:ListItem>
                <asp:ListItem Value="ND">North Dakota</asp:ListItem>
                <asp:ListItem Value="OH">Ohio</asp:ListItem>
                <asp:ListItem Value="OK">Oklahoma</asp:ListItem>
                <asp:ListItem Value="OR">Oregon</asp:ListItem>
                <asp:ListItem Value="PA">Pennsylvania</asp:ListItem>
                <asp:ListItem Value="RI">Rhode Island</asp:ListItem>
                <asp:ListItem Value="SC">South Carolina</asp:ListItem>
                <asp:ListItem Value="SD">South Dakota</asp:ListItem>
                <asp:ListItem Value="TN">Tennessee</asp:ListItem>
                <asp:ListItem Value="TX">Texas</asp:ListItem>
                <asp:ListItem Value="UT">Utah</asp:ListItem>
                <asp:ListItem Value="VT">Vermont</asp:ListItem>
                <asp:ListItem Value="VA">Virginia</asp:ListItem>
                <asp:ListItem Value="WA">Washington</asp:ListItem>
                <asp:ListItem Value="WV">West Virginia</asp:ListItem>
                <asp:ListItem Value="WI">Wisconsin</asp:ListItem>
                <asp:ListItem Value="WY">Wyoming</asp:ListItem>
            </asp:DropDownList>
            <br /><br />
            <asp:Label ID="lblRestZip" runat="server" CssClass="left">Zip Code: </asp:Label>
            <asp:TextBox ID="txtRestZip" runat="server" CssClass="right"></asp:TextBox>
            <br /><br />
            <asp:Label ID="lblCategory" runat="server" CssClass="left">Category: </asp:Label>
            <asp:DropDownList ID="ddCategories" runat="server" CssClass="right" Width="140px">
                <asp:ListItem Value="1">American</asp:ListItem>
                <asp:ListItem Value="2">Mexican</asp:ListItem>
                <asp:ListItem Value="3">Korean</asp:ListItem>
                <asp:ListItem Value="4">Chinese</asp:ListItem>
                <asp:ListItem Value="5">Japanese</asp:ListItem>
            </asp:DropDownList>
            <br /><br />
            <asp:Button ID="btnSubmit" Text="Submit" runat="server" CssClass="button2" OnClick="btnSubmit_Click" />
            <asp:Button ID="btnReview" Text="Add Review" runat="server" OnClick="btnReview_Click" CssClass="button2" />
        </fieldset>

1 个答案:

答案 0 :(得分:0)

我发现了我的错误。我打开了两次连接。

objDB.GetConnection().Open();

将其更改为:

objDB.GetConnection();