【问题标题】:Trying to call the stored procedure from C# windows form but nothing happens尝试从 C# windows 窗体调用存储过程,但没有任何反应
【发布时间】:2015-04-07 20:50:09
【问题描述】:

我正在尝试从 C# windows 窗体应用程序调用存储过程。它被执行但在数据库中没有发生任何事情,即没有记录被添加到表中。 这是我的 C# 代码:

private void buttonAddData_Click(object sender, EventArgs e)
{
    string actionTaken = "Added";
    string changeDescription = "Added new Code";
    string updatedBy = "ICherry";
    string currentStatus = "In development";
    SqlConnection conDatabase = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\ConfigData.mdf;Integrated Security=True;");
    try
    {
        SqlCommand cmd = conDatabase.CreateCommand();
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "spLogConfigChanges";                
        cmd.Parameters.AddWithValue("@ActionTaken",actionTaken);
        cmd.Parameters.AddWithValue("@ChangeDescription", changeDescription);
        cmd.Parameters.AddWithValue("@UpdatedBy", updatedBy);
        cmd.Parameters.AddWithValue("@CurrentStatus", currentStatus);
        conDatabase.Open();
        MessageBox.Show("Connection opened");
        cmd.ExecuteNonQuery();
        MessageBox.Show("Command Executed");
        conDatabase.Close();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}    

我的 SP 在查询分析器中运行时没有错误,我可以看到正在创建的记录。

【问题讨论】:

  • @paqogomez- 是的,它在 SSMS 中运行良好。
  • 创建命令前需要打开连接吗?我正在看一堆例子来说明这一点。此外,如果您将 SqlConnection 包装在 using 语句中,则不必担心调用 Close(),或者如果抛出异常则不会调用 Close()
  • 使用查询分析器时参数是一样的吗?
  • cmd.ExecuteNonQuery(); 返回什么。它是否返回 -1
  • @Bond- 是的,当我在查询分析器中运行 sp 时,参数是相同的

标签: c# winforms stored-procedures


【解决方案1】:

而不是使用

        SqlCommand cmd = conDatabase.CreateCommand();
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "spLogConfigChanges";                
        cmd.Parameters.AddWithValue("@ActionTaken",actionTaken);
        cmd.Parameters.AddWithValue("@ChangeDescription", changeDescription);
        cmd.Parameters.AddWithValue("@UpdatedBy", updatedBy);
        cmd.Parameters.AddWithValue("@CurrentStatus", currentStatus);
        conDatabase.Open();
        MessageBox.Show("Connection opened");
        cmd.ExecuteNonQuery();
        MessageBox.Show("Command Executed");
        conDatabase.Close();

试试这个:

conDatabase.Open();
    SqlCommand cmd = new SqlCommand("spLogConfigChanges",conDatabase);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("@ActionTaken",SqlDbType.Varchar).Value = actionTaken;
cmd.Parameters.Add("@ChangeDescription",SqlDbType.VarChar).Value = changeDescription;
cmd.Parameters.Add("@UpdateBy",SqlDbType.VarChar).Value = updatedBy;
cmd.Parameters.Add("@CurrentStatus",SqlDbType.VarChar).Value=currentStatus);

cmd.ExecuteNonQuery();
MessageBox.Show("Command Executed");
conDatabase.Close();

如果遇到错误请评论

【讨论】:

  • 我没有看到任何错误,但没有任何错误被添加到数据库中。
  • @MethodMan- 当我在 localDB 之前添加 '\\' 时,它显示“在建立与 SQL Server 的连接时发生网络相关或特定于实例的错误。服务器未找到或无法访问。'所以,可能它不喜欢连接字符串中的'\\'。
  • @iCherry 你在使用 SQL Server 吗?如果是,请将连接字符串更改为 @"DataSource = LocalDB;Initial Catalog=DatabaseName;IntegratedSecutiy=TRUE";
【解决方案2】:

我在使用stored procedure 时遇到了同样的问题,我解决了它

改变这个

cmd.Parameters.AddWithValue("@param", "String Value")

到此

cmd.Parameters.Add("@param", OleDbType.VarChar).Value = "String Value";

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-07-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-05-13
    • 1970-01-01
    • 2021-05-13
    • 2012-12-20
    相关资源
    最近更新 更多