我有followind存储过程:
CREATE PROCEDURE [dbo].[SD_languagageDontMatch]
(@param1 NVARCHAR(150))
AS
BEGIN
SET NOCOUNT ON;
SELECT *
FROM Languages
WHERE Languages.ID_language not in (SELECT editorLanguage.IDlanguage FROM editorLanguage where editorLanguage.IDuser = @param1)
END
以下代码:
string strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString;
SqlConnection sqlConnection1 = new SqlConnection(strConnString);
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "SD_languagageDontMatch";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@param1", HttpContext.Current.User.Identity.GetUserId());
SqlDataReader reader;
cmd.Connection = sqlConnection1;
cmd.CommandType = CommandType.Text;
cmd.Connection = sqlConnection1;
sqlConnection1.Open();
reader = cmd.ExecuteReader();
//do stuff
reader.Close();
sqlConnection1.Close();
sqlConnection1.Dispose();
每次运行代码时都会出现以下错误:
程序或功能' SD_languagageDontMatch'期望参数 ' @ param1',未提供。
之前我曾使用过存储过程,但之前我从未遇到过这种情况。一切似乎都是正确的。我直接在服务器上执行了存储过程并且运行良好。我的代码中是否有任何遗漏?
由于
更新
我在声明参数之前打开SQL连接解决了问题:
sqlConnection1.Open();
cmd.CommandText = "SD_langDontMatch";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@param1", HttpContext.Current.User.Identity.GetUserId());
SqlDataReader reader;
reader = cmd.ExecuteReader();