数值超出范围

编程入门 行业动态 更新时间:2024-10-25 22:24:03
本文介绍了数值超出范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在激励下面的代码

I''m excuting following code

OdbcConnection con; OdbcCommand cmd; con = new OdbcConnection(@"Dsn=chaitudi;dbq=C:\project\Distributor.accdb;driverid=25;fil=MS Access;maxbuffersize=2048;pagetimeout=5;uid=admin"); con.Open(); cmd = new OdbcCommand("insert into Company(ID,CompanyName,StreetName,City,ZipCode,State,TelePhone) values(''" +textBox12.Text + "'',''" + textBox4.Text + "'',''" + textBox5.Text + "'',''" + textBox6.Text + "'',''" + textBox7.Text + "'',''" + textBox8.Text + "'',''" + textBox10.Text + "'')", con); cmd.ExecuteNonQuery(); con.Close(); MessageBox.Show("Stroed Successfully");

得到错误数值超出范围...... plz帮助我...... 提前提交

Bt getting the error "Numeric value out of range"... plz help me... thanx in advance

推荐答案

因为那里唯一的数字是微不足道的:25和2048它必须是你试图从文本框中插入的值。所以首先根据数据库中的数据类型检查它们。 但不要这样做!不要连接字符串以构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。改为使用参数化查询。 另外,你应该处理你的连接和命令 - 它们是稀疏商品,因此它们不应该保存得比你需要的时间长。 哦,并且停止使用控件的VS默认名称:你可能还记得今天textbox12拥有用户ID,但你不会在几周你需要保持这个。称呼他们是明智的:它使您的代码更易于阅读,理解和维护。输入也更快,因为知识产权可以更快地为它们排序... Since the only numbers in there are trivial: "25" and "2048" it has to be the values you are trying to insert from your textboxes. So start by checking them against the datatypes in your database. But don''t do it that way! Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead. Also, you should be disposing your connections and commands - they are scarse commodities, so they should not be kept longer than you need to. Oh, and stop using the VS default names for controls: you may remember today that "textbox12" holds the user ID, but you won''t in a coupe of weeks time when you need to maintain this. Call them somethign sensible: it makes your code easier to read, understand and maintain. It is also quicker to type, since intelisense can sort them out quicker for you... using (OdbcConnection con = new ODbcConnection(strConnect)) { con.Open(); using (OdbcCommand com = new OdbcCommand("INSERT INTO Company (ID,CompanyName) VALUES (@UID, @CON)", con)) { com.Parameters.AddWithValue("@UID", tbUserID.Text); com.Parameters.AddWithValue("@CON", tbCompanyName.Text); com.ExecuteNonQuery(); } }

更多推荐

数值超出范围

本文发布于:2023-10-16 12:20:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1497538.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数值

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!