带用户输入的顺序存储过程

时间:2016-08-05 15:58:22

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

我有一个Web应用程序,它接受用户输入并从用户输入生成图像。这些图像应按顺序生成,并且需要每天重置。

我试图至少将序列值,序列和日期存储到数据库中,但它不会更新。

到目前为止,我的数据库不会更新,我的代码可以运行,但是无法正常运行。我不确定我的问题在哪里。我在网上找到了类似的帮助,但它们似乎没有用。

存储过程:

ALTER PROCEDURE barcode_insert(
    @Seq_Num int,
    @date datetime,
    @ImageName varchar
)
AS
BEGIN
    SET NOCOUNT ON

    UPDATE ImageInfoTable
    SET imagedate = @date,ImageNum = @Seq_Num
    WHERE image_name = @ImageName
END

RETURN @Seq_Num

C#代码

protected void gen_barcode(object sender, EventArgs e)
{
    int n;
    int i = Int32.Parse(amount.Text);

    string date_picker = datepicker.Text;

    SqlConnection conn = new SqlConnection(GetConnectionString());

    SqlCommand cmd = new SqlCommand();
    cmd.Connection = conn;

    cmd.CommandType = CommandType.StoredProcedure;
    cmd.CommandText = "barcode_insert"

    cmd.Parameters.AddWithValue("@Seq_Num", amount.Text);
    cmd.Parameters.AddWithValue("@date", date_picker);
    cmd.Parameters.AddWithValue("@ImageName", CheckBox.Checked);

    if (CheckBox_Code.Checked)
    {
          //generate image code
    }

    cmd.Connection.Open();
    cmd.ExecuteNonQuery();
    conn.Close();
}    

1 个答案:

答案 0 :(得分:0)

尝试明确设置参数类型。

protected void gen_barcode(object sender, EventArgs e)
{
    DateTime date_picker = datepicker.Value;
    int intAmount; // get the int value for the amount here...
    String imgName; // get the image name here...
    SqlConnection conn = new SqlConnection(GetConnectionString());

    SqlCommand cmd = new SqlCommand();
    cmd.Connection = conn;

    cmd.CommandType = CommandType.StoredProcedure;
    cmd.CommandText = "barcode_insert"

    cmd.Parameters.Add("@Seq_Num", SqlDbType.Int);
    cmd.Parameters.Add("@date", SqlDbType.DateTime);
    cmd.Parameters.Add("@ImageName" , SqlDbType.NVarChar);
    cmd.Parameters["@Seq_Num"].Value = intAmount;
    cmd.Parameters["@date"].Value = date_picker;
    cmd.Parameters["@ImageName"].Value = imgName;

    if (CheckBox_Code.Checked)
    {
          //put this somewhere else since it isn't related in function
    }

    cmd.Connection.Open();
    cmd.ExecuteNonQuery();
    conn.Close();
}