Oledb异常错误 [英] Oledb Exception Error

查看:78
本文介绍了Oledb异常错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hi

i面临oledb异常错误,源代码在下面

hi
i am facing oledb exception error the source code is below

 string query = "Insert into RealData(WantTo,PropertyType,Names,Owner,Area,
CateType,Address,Price,Desc)values('" + cmbWantTO.SelectedItem.ToString() + "',
'" + protery + "','" + txtName.Text + "','" + cmbType.SelectedItem.ToString() + "',
'" + cmbArea.SelectedItem.ToString() + "','" + cmbCategoeryType.SelectedItem.ToString() + "','" + address + "',
'" + txtPrice.Text + "','" + txtPDesc.Text + "')";
 cmd = new OleDbCommand(query, cnn);
int xy = cmd.ExecuteNonQuery();







此代码将异常插入语句语法错误plz帮助我。




this code is giving exception insert into statement syntax error plz help me.

推荐答案

您没有适当地给出空格来进行正确的查询。

问题:

You have not given spaces appropriately to make correct query.
Issues:
RealData(WantTo,
Desc)values('



尝试:


Try:

string query = "INSERT INTO RealData (WantTo, PropertyType, Names, Owner, Area, CateType, Address, Price, Desc) VALUES ('" + cmbWantTO.SelectedItem.ToString() + "',
'" + protery + "','" + txtName.Text + "','" + cmbType.SelectedItem.ToString() + "',
'" + cmbArea.SelectedItem.ToString() + "','" + cmbCategoeryType.SelectedItem.ToString() + "','" + address + "',
'" + txtPrice.Text + "','" + txtPDesc.Text + "')";
 cmd = new OleDbCommand(query, cnn);
int xy = cmd.ExecuteNonQuery();





顺便说一下,你的实现是针对SQL注入开放的,根本不可建议。您应该使用参数化查询。

在此处阅读有关保护SQL注入的信息: SQL注入缓解:使用参数化查询 [ ^ ]


为了爱你的理智和你的同事谁必须看看这个,通过内联sql你的杀戮可维护性...至少做string.format



我重新安排你的sql使用string.format。试一试我不知道为什么这不会起作用,除非你的sql中的列名与表中的列名不匹配。在这种情况下,你没有在你的问题中指明这一点,所以谁知道那个问题是什么......可能是外星人拿走了你的桌子。



For the love of your sanity and your coworkers who have to look at this, your killing maintainability by doing inline sql...at the bare minimum do string.format

I re-arranged your sql using string.format. Give it a shot i dont see why this wouldnt work unless your column names in the sql are not match what is in your table. In which case you didnt specify that in your question so who knows what the problem is at that point...it could be the aliens took your table.

string query = string.Format(@"INSERT INTO RealData (WantTo,PropertyType,Names,Owner,Area,CateType,Address,Price,Desc) VALUES ('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}')", cmbWantTO.SelectedItem.ToString(),
                                                                                                                                                                                         protery,
                                                                                                                                                                                         txtName.Text,
                                                                                                                                                                                         cmbType.SelectedItem.ToString(),
                                                                                                                                                                                         cmbArea.SelectedItem.ToString(),
                                                                                                                                                                                         cmbCategoeryType.SelectedItem.ToString(),
                                                                                                                                                                                         address,
                                                                                                                                                                                         txtPrice.Text,
                                                                                                                                                                                         txtPDesc.Text);


这篇关于Oledb异常错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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