数值超出范围 [英] Numeric value out of range

查看:90
本文介绍了数值超出范围的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在激励下面的代码



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();
        }
    }


这篇关于数值超出范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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