插入数据时出现语法异常 [英] Syntax exception coming while inserting data

查看:70
本文介绍了插入数据时出现语法异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



每当我尝试将数据插入Access db时,通过我的语法错误异常,我尝试了一切,但仍然出现错误



我尝试过:



插入代码:

cmd = new OleDbCommand(插入RegistrationForm(ClientCount,Name,Address,Muddat,Money_Return_date,Account_status,Taking_Amout,Interest_per_month,Pending_interest_month,Pending_interst_Amount,Total_Amount,Client_image,Document_image1,Document_image2)值(+ lblcount) .Text +','+ textBox20.Text +','+ textBox21.Text +','+ textBox19.Text +','+ textBox18.Text +','+ maskedTextBox1 .Text.ToString()+','+ textBox22.Text +','+ maskedTextBox2.Text.ToString()+','+ textBox23.Text +','+ textBox17.Text +','+ textBox16.Text +','+ textBox15.Text +','+ Convert.ToDouble(textBox13.Text)+','+ Convert.T oDouble(textBox14.Text)+',@ pictureBox6,@ pictureBox4,@ pictureBox5)',con);

conv_photo();

con.Open() ;

int n = cmd.ExecuteNonQuery(); - 这条线上的例外

con.Close();

if(n> 0)

{

MessageBox.Show(记录已插入);

loaddata();

rno ++;

}

else

MessageBox.Show(插入失败);



conv_photo方法:



public void conv_photo()

{

//将照片转换为二进制数据





if(pictureBox6.Image!= null)

{

//使用MemoryStream:

ms = new MemoryStream();

pictureBox6.Image.Save(ms,System.Drawing.Imaging.ImageFormat.Jpeg);

byte [] photo_aray = new byte [ms。长度];

ms.Position = 0;

ms.Read(photo_aray,0,photo_aray.Length);

cmd.Parameters.AddWithValue(@ pictureBox6,photo_aray);

}



if(pictureBox4.Image!= null)

{

//使用MemoryStream:

ms = new MemoryStream();

pictureBox4.Image.Save(ms,System.Drawing.Imaging.ImageFormat.Jpeg);

byte [] photo_aray = new byte [ms .Length];

ms.Position = 0;

ms.Read(photo_aray,0,photo_aray.Length);

cmd.Parameters .AddWithValue(@ pictureBox4,photo_aray);

}

if(pictureBox5.Image!= null)

{

//使用MemoryStream:

ms = new MemoryStream();

pictureBox5.Image.Save(ms,System.Drawing.I maging.ImageFormat.Jpeg);

byte [] photo_aray = new byte [ms.Length];

ms.Position = 0;

ms.Read(photo_aray,0,photo_aray.Length);

cmd.Parameters.AddWithValue(@ pictureBox5,photo_aray);

}

}



输出为:



'2','Atul','india', '4589787978', '收集', '2015年12月5日', '4', '2015年12月6日', '有效', '5000', '500', '5', '2500','7500 ',@ pictureBox6,@ pictureBox4,@ pictureBox5''。

hi all,

Whenever i try to insert data into Access db its through me syntax error exception, i tried everything but still error is coming

What I have tried:

Insert code:
cmd = new OleDbCommand("insert into RegistrationForm(ClientCount,Name,Address,Contact,Documents,Money_Taking_Date,Muddat,Money_Return_date,Account_status,Taking_Amout,Interest_per_month,Pending_interest_month,Pending_interst_Amount,Total_Amount,Client_image,Document_image1,Document_image2) values(" + lblcount.Text + "','" + textBox20.Text + "','" + textBox21.Text + "','" + textBox19.Text + "','" + textBox18.Text + "','" + maskedTextBox1.Text.ToString() + "','" + textBox22.Text + "','" + maskedTextBox2.Text.ToString() + "','" + textBox23.Text + "','" + textBox17.Text + "','" + textBox16.Text + "','" + textBox15.Text + "','" + Convert.ToDouble(textBox13.Text) + "','" + Convert.ToDouble(textBox14.Text) + "',@pictureBox6,@pictureBox4,@pictureBox5)'", con);
conv_photo();
con.Open();
int n = cmd.ExecuteNonQuery(); -- exception coming on this line
con.Close();
if (n > 0)
{
MessageBox.Show("record inserted");
loaddata();
rno++;
}
else
MessageBox.Show("insertion failed");

conv_photo method:

public void conv_photo()
{
//converting photo to binary data


if (pictureBox6.Image != null)
{
//using MemoryStream:
ms = new MemoryStream();
pictureBox6.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] photo_aray = new byte[ms.Length];
ms.Position = 0;
ms.Read(photo_aray, 0, photo_aray.Length);
cmd.Parameters.AddWithValue("@pictureBox6", photo_aray);
}

if (pictureBox4.Image != null)
{
//using MemoryStream:
ms = new MemoryStream();
pictureBox4.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] photo_aray = new byte[ms.Length];
ms.Position = 0;
ms.Read(photo_aray, 0, photo_aray.Length);
cmd.Parameters.AddWithValue("@pictureBox4", photo_aray);
}
if (pictureBox5.Image != null)
{
//using MemoryStream:
ms = new MemoryStream();
pictureBox5.Image.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
byte[] photo_aray = new byte[ms.Length];
ms.Position = 0;
ms.Read(photo_aray, 0, photo_aray.Length);
cmd.Parameters.AddWithValue("@pictureBox5", photo_aray);
}
}

output is :

'2','Atul','india','4589787978','collected','12/05/2015','4','12/06/2015','Active','5000','500','5','2500','7500',@pictureBox6,@pictureBox4,@pictureBox5)'.

推荐答案

在第十亿次问题这个问题被提出后,谷歌要求C#参数化查询来学习如何解决这个小问题。



另外,你可能想谷歌SQL注入攻击找出为什么使用字符串连接来构建你的SQL语句风险破坏你的整个数据库。
For the umpteen billionth time this question has been asked, Google for "C# parameterized queries" to learn how to fix this little problem.

Also, you might want to Google for "SQL Injection Attack" to find out why using string concatenation to build your SQL statements risks destroying your entire database.


除了戴夫所说的,请看这里:为什么我得到参数无效。我从数据库中读取图像时出现异常? [ ^ ] - 它显示了如何在DB中保存图像。
In addition to what Dave says, have a look here: Why do I get a "Parameter is not valid." exception when I read an image from my database?[^] - it shows how to save an image in a DB.


这篇关于插入数据时出现语法异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆