尝试从 C# windows 表单调用存储过程但没有任何反应

Trying to call the stored procedure from C# windows form but nothing happens

我正在尝试从 C# windows Form 应用程序调用存储过程。它被执行但数据库中没有任何反应,即没有记录被添加到 table。 这是我的 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 在查询分析器中运行时没有错误,我可以看到正在创建的记录。

而不是使用

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

如果遇到错误请评论

我在使用stored procedure时遇到了同样的问题,我通过

解决了

改变这个

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

至此

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