1

我有这个代码:

 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 数据库中。我只是得到这个令人沮丧的错误

4

1 回答 1

0

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

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

类似问答:OleDbCommand parameters order and priority

于 2013-07-06T14:44:11.030 回答