必须声明变量标量

时间:2013-07-05 20:00:11

标签: sql sql-server error-handling scalar

我有这段代码:

 private void btGuardar_Click(object sender, EventArgs e)
        {
            if (txDescrip.Text.Trim().Equals("") == false && txPath.Text.Trim().Equals("") == false)
            {
                try
                {
                    byte[] imgData = getMyFileBytes(txPath.Text);
                    //Server connection
                    OleDbConnection connection = new OleDbConnection(strcx);
                    String q = "INSERT INTO MisImagenes (Id,CustomImage) values(@MyPath, @ImageData)";
                    //Initialize sql command object for insert
                    OleDbCommand command = new OleDbCommand(q, connection);
                    //We are passing original image path and image byte data as sql parameters
                    OleDbParameter pMyPath = new OleDbParameter("@MyPath", (object)txPath.Text);
                    OleDbParameter pImgData = new OleDbParameter("@ImageData", (object)imgData);
                    command.Parameters.Add(pMyPath);
                    command.Parameters.Add(pImgData);
                    //Open connection and execute insert query
                    connection.Open();
                    command.ExecuteNonQuery();
                    connection.Close();
                    Mensaje.aviso("Imagen Guardada :)");
                    //Limpiamos
                    clearAlta();
                }
                catch (Exception exc)
                {
                    Mensaje.aviso("Something went wrong! :( " + exc.Message);
                }
            }
        }

当我执行此操作时说"必须声明标量变量" @ MyPath"。" ......有什么帮助吗?拜托,谢谢。

我只是想通过选择图像的路径和ID描述来将图像保存到我的sqlserver数据库。我只是得到了这个令人沮丧的错误

1 个答案:

答案 0 :(得分:0)

你应该使用'?'而不是oledb查询中的参数名称

INSERT INTO MisImagenes (Id,CustomImage) values(?, ?)

类似的问答:OleDbCommand parameters order and priority