如何解决这个错误,请 [英] How to solve this error ,please

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

问题描述

Hii,每个人我都是初学者
我需要知道这是访问2010的sql语句的真实语法.
因为它给我错误(输入字符串的格式不正确.)

Hii ,everyone i''m beginner
i need to know this is the true syntax of sql statement to access 2010
because it give me error (input string was not in correct format.)

oda.InsertCommand = new OleDbCommand("INSERT INTO student VALUES (@id_civil_st,@names,@phone,@address,@school_year,@amount_paid,@amount_rest,@date_record,@date_start,@date_end)", con);
           oda.InsertCommand.Parameters.Add("@id_civil_st", OleDbType.Integer).Value =Convert.ToInt32( textBox1.Text);
           oda.InsertCommand.Parameters.Add("@names", OleDbType.Char).Value =Convert.ToString( textBox2.Text);

推荐答案

可能是,您的一个文本框没有包含可正确转换的值.如果textBox1不包含数字,则转换操作将失败,并显示该错误消息.我建议您在设置InsertCommand之前先执行一堆操作或TryParse操作,以便可以更准确地报告问题:
Probably, one of your textboxes does not contain a value that converts properly. If textBox1 does not hold a number, the Convert operation will fail with that error message. I would suggest doing a bunch or TryParse operations first, before you set up the InsertCommand, so you can report problems more accurately:
int idCivilSt;
if (!Int.TryParse(textBox1.Text, out idCivilSt))
   {
   // Report the problem and exit method
   }



其他几件事:
1)不要对项目名称使用VS默认值:今天您可能还记得textBox2拥有一个名称,但是您还记得下个月何时需要进行更改吗?还是您必须浪费时间参考设计?改用tbNames,您无需记住-代码变得更加清晰易读.
2)您不需要将字符串转换为字符串-浪费时间并且看起来很傻!
3)尝试使用Parameters.AddWithValue而不是Parameters.Add-再次使代码更具可读性:



A couple of other things:
1) Don''t use VS defaults for item names: you may remember today that textBox2 holds a name, but will you remember next month, when you have to make changes? Or will you have to waste time referring back to designs? Use tbNames instead, and you don''t need to remember - the code becomes musch clearer and easier to read.
2) YOu don''t need to convert strings to strings - it wastes time and looks silly!
3) Try using Parameters.AddWithValue instead of Parameters.Add - again, it makes code more readable:

oda.InsertCommand.Parameters.AddWithValue("@names", textBox2.Text);

(或者更好)

(Or better still)

oda.InsertCommand.Parameters.AddWithValue("@names", tbNames.Text);


更具可读性

Is a lot more readable than

oda.InsertCommand.Parameters.Add("@names", OleDbType.Char).Value =Convert.ToString( textBox2.Text);


4)在编写INSERT或UPDATE(或SELECT)语句时,请始终列出字段名称.如果您不这样做(因为您不这样做),那么对数据库的更改可能会破坏您的代码,这取决于现有的字段顺序,将来可能无法维护.


4) Always list the field names when you write an INSERT or UPDATE (or SELECT for that matter) statement. If you don''t (as you don''t) then a change to the database can break your code, which relies on an existing field order, which may not be maintained in future.

oda.InsertCommand = new OleDbCommand("INSERT INTO student (Id, Name, Phone, Address, Year, AmountPaid, AmountRest, DateRecird, DateStart, DateEnd) VALUES (@id_civil_st,@names,@phone,@address,@school_year,@amount_paid,@amount_rest,@date_record,@date_start,@date_end)", con);

安全得多,并且还可以检查数据库中的数据之前,您没有错过任何一项.

Is a lot safer, and also provides a check that you haven''t missed one out before you mess up data in your database.


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

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