使用SQL Server中的存储过程从数据库获取数据

时间:2015-12-10 03:36:40

标签: html sql asp.net vb.net sql-server-2012

我正在设计一个Web应用程序。我创建了表和列。我试图使用asp.net插入用户详细信息。但是不在SQL Server中显示用户详细信息并且快乐的事情是没有错误。谁能告诉我这是什么原因?

这是btnSave__Click的代码:

Private Sub btnSave_Click(sender As Object, e As EventArgs) HandlesbtnSave.Click
    Dim recepient As String = CheckChanges()
    Dim cn As New SqlConnection

    cn.ConnectionString = conStrDRS

    Using cn
        cn.Open()
        Dim cmd As SqlCommand = New SqlCommand("UpdateRegistration", cn)
        cmd.CommandType = CommandType.StoredProcedure
        cmd.Parameters.AddWithValue("@Title", txtTitle.Text)

        cmd.Parameters.AddWithValue("@CounterParty", txtCounterParty.Text)
        cmd.Parameters.AddWithValue("@Company", ddlCompany.SelectedValue)
        cmd.Parameters.AddWithValue("@Custodian", ddlCustodian.SelectedValue)
        cmd.Parameters.AddWithValue("@CreatedOn", DateTime.ParseExact(YourDate, "ddd MMM dd HH:mm:ss KKKK yyyy", CultureInfo.InvariantCulture))
        cmd.Parameters.AddWithValue("@OwnerDepartment", ddlOwnerDepartment.SelectedValue)
        cmd.Parameters.AddWithValue("@CreatedBy", getUserName(hUserID.Value, "name"))
        cmd.Parameters.AddWithValue("@Status", txtStatus.Text)
        cmd.Parameters.AddWithValue("@Remarks", txtRemarks.Text)
        cmd.Parameters.AddWithValue("@SecurityLevel", ddlSecurityLevel.SelectedValue)

cm.ExecuteNonQuery()         结束使用         cn.Close()     结束子

这是我的存储过程:

ALTER PROCEDURE [dbo].[UpdateRegistration]
   (@Title nvarchar(500),
    @CounterParty nvarchar(200),
    @Company nvarchar(10),
    @Custodian varchar(25),
    @OwnerDepartment  varchar(100),
    @Remarks nvarchar(1000),
    @Status varchar(2),
    @SecurityLevel varchar(2),
    @CreatedBy varchar(25),
    @CreatedOn datetime)
AS
BEGIN
    SET NOCOUNT ON;

    INSERT INTO dbo.Registration(Title, Counterparty, Company,
                                 Custodian, OwnerDepartment, Remarks,
                                 Status, SecurityLevel, CreatedBy, CreatedOn)
    VALUES (@Title, @Counterparty, @Company,
            @Custodian, @OwnerDepartment, @Remarks,
            @Status, @SecurityLevel, @CreatedBy, @CreatedOn)
END

提前致谢!

2 个答案:

答案 0 :(得分:1)

您必须执行Sql命令

您缺少cmd.ExecuteNonQuery();

 Private Sub btnSave_Click(sender As Object, e As EventArgs) HandlesbtnSave.Click
        Dim recepient As String = CheckChanges()
         Dim cn As New SqlConnection
        cn.ConnectionString = conStrDRS
        Using cn
            cn.Open()
            Dim cmd As SqlCommand = New SqlCommand("UpdateRegistration", cn)
            cmd.CommandType = CommandType.StoredProcedure
            cmd.Parameters.AddWithValue("@Title", txtTitle.Text)
            cmd.Parameters.AddWithValue("@ReferenceNo", txtReferenceNo.Text)
            cmd.Parameters.AddWithValue("@CounterParty", txtCounterParty.Text)
            cmd.Parameters.AddWithValue("@Company", ddlCompany.SelectedValue)
            cmd.Parameters.AddWithValue("@Custodian", ddlCustodian.SelectedValue)
            cmd.Parameters.AddWithValue("@CreatedOn", DateTime.ParseExact(YourDate, "ddd MMM dd HH:mm:ss KKKK yyyy", CultureInfo.InvariantCulture))
            cmd.Parameters.AddWithValue("@OwnerDepartment", ddlOwnerDepartment.SelectedValue)

   string user = getUserName(hUserID.Value, "name");
   user == null ? cmd.Parameters.AddWithValue("@CreatedBy",DBNull.Value):
                 cmd.Parameters.AddWithValue("@CreatedBy",user);
            cmd.Parameters.AddWithValue("@Status", txtStatus.Text)
            cmd.Parameters.AddWithValue("@Remarks", txtRemarks.Text)
            cmd.Parameters.AddWithValue("@SecurityLevel", ddlSecurityLevel.SelectedValue)
            cmd.ExecuteNonQuery(); //Execute the stored procedure
        End Using
        cn.Close()
    End Sub

答案 1 :(得分:0)

您缺少cmd.ExecuteNonQuery();以及传递一个存储过程中不存在的额外参数

cmd.Parameters.AddWithValue("@ReferenceNo", txtReferenceNo.Text)

所以就像这样:

Private Sub btnSave_Click(sender As Object, e As EventArgs) HandlesbtnSave.Click
  Dim recepient As String = CheckChanges()
  Dim cn As New SqlConnection
  cn.ConnectionString = conStrDRS

  Using cn
    cn.Open()
    Dim cmd As SqlCommand = New SqlCommand("UpdateRegistration", cn);
    cmd.CommandType = CommandType.StoredProcedure
    cmd.Parameters.AddWithValue("@Title", txtTitle.Text);
    cmd.Parameters.AddWithValue("@CounterParty", txtCounterParty.Text);
    cmd.Parameters.AddWithValue("@Company", ddlCompany.SelectedValue);
    cmd.Parameters.AddWithValue("@Custodian", ddlCustodian.SelectedValue);
    cmd.Parameters.AddWithValue("@CreatedOn", DateTime.ParseExact(YourDate,
    "ddd MMM dd HH:mm:ss KKKK yyyy", CultureInfo.InvariantCulture));
    cmd.Parameters.AddWithValue("@OwnerDepartment", ddlOwnerDepartment.SelectedValue);
    cmd.Parameters.AddWithValue("@CreatedBy", getUserName(hUserID.Value, "name"));
    cmd.Parameters.AddWithValue("@Status", txtStatus.Text);
    cmd.Parameters.AddWithValue("@Remarks", txtRemarks.Text);
    cmd.Parameters.AddWithValue("@SecurityLevel",
    ddlSecurityLevel.SelectedValue);
    cmd.ExecuteNonQuery();
  End Using
  cn.Close()
End Sub