插入数据库将别名更改为有效名称?

时间:2013-10-30 06:44:06

标签: c# sql winforms

我有一个sql表,它有一个图像cloumn我试图插入图像后将其转换为byte ....我的插入查询看起来像这样

   INSERT INTO Member (F_NAME, L_NAME, D_O_B, UAE_ID_NO, MOBILE_NO, EMAIL_ID, REFERER, REF_CONTACT, ADDRESS, PICTURE) VALUES ('" + fname + "','" + lname + "','" + dob.ToShortDateString() + "','" + uaeid + "','" + mobile + "','" + emailid + "','" + reffere + "','" + refercontact + "','"+address+"',"+photo+")"

这里pic是图像列...我正在使用以下方法将图像转换为字节数组

    MemoryStream ms = new MemoryStream();
            //save the image into memory stream
            pBoxMember.Image.Save(ms, ImageFormat.Jpeg);
            //assign the byte array with total size of memorystream
            photo = new byte[ms.Length];
            ms.Position = 0;
            ms.Read(photo, 0, photo.Length);
            return true;

但它给了我这个错误 对象或列名称缺失或为空。对于SELECT INTO语句,请验证每列是否具有名称。对于其他语句,请查找空别名。不允许使用定义为“”或[]的别名。将别名更改为有效名称。 “。

附近的语法无效

1 个答案:

答案 0 :(得分:0)

如果这应该起作用,应该如下所示:

var sql = "INSERT INTO Member (F_NAME, L_NAME, D_O_B, UAE_ID_NO, MOBILE_NO, " + 
"EMAIL_ID, REFERER, REF_CONTACT, ADDRESS, PICTURE) " + 
"VALUES ('" + fname + "','" + lname + "','" + dob.ToShortDateString() + "','" + uaeid + "','" + mobile + "','" + emailid + "','" + reffere + "','" + refercontact + "','" + address + "'," + photo + ")"

但你应该使用SQL Params,例如:

string queryString = "INSERT INTO Member (F_NAME, L_NAME, D_O_B, UAE_ID_NO, ... VALUES(@fname ,@lname ....)";
SqlCommand cmd = new SqlCommand(queryString, dbConnection);
cmd.Parameters.AddWithValue("@fname",fname);
cmd.Parameters.AddWithValue("@lname",lname);
...
cmd.Parameters.AddWithValue("@blobParam",YourBytesArray);
...
cmd.ExecuteNonQuery();