','附近的语法不正确。数据在数字字段中插入问题。 [英] Incorrect syntax near ','. data insert problems in number field.

查看:46
本文介绍了','附近的语法不正确。数据在数字字段中插入问题。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,





srno号码(18,0)

名称varchar(50 )



i有两个文本框。当我填充文本框然后填写数据。当我只填充srno然后还输入数据。但是当我只填写名字并将srno留空时,错误::'''附近的语法不正确。







我的代码是....



hi all,

table
srno number(18,0)
name varchar(50)

i have two textboxes.when i fill both textbox then data insert. when i fill only srno then also data enter. but when i fill only name and leave srno blank then, error :: Incorrect syntax near ','.



MY CODE IS....

con.Open()
ss = "insert into fir values("& TextBox1.Text &",' " & textBox2.Text & " ')"
com = New SqlCommand(ss, con)
com.ExecuteNonQuery()
MsgBox("Data Stored Successfully")
con.Close()

推荐答案

Google用于.net参数化查询。现在,任何人都可以通过在输入中添加'来搞砸你的查询。这是所谓的SQL注入攻击的基础之一。
Google for ".net parameterized query". Right now, anyone can screw up your query just by adding a "'" to the input. This is one of the basis for what's called a SQL Injection attack.


在你离开Textboxfield的第二种情况下(SrNo为空)

然后它通过 对于与你的Decimal字段不兼容的数据库

sr.No(十进制(18,0)

因此需要在将它传递到数据库之前进行检查,如下所示

In Your Second Case when You leave your Textboxfield (SrNo empty)
then it passes "" to the databse which is incompatible with your Decimal field
sr.No(Decimal(18,0)
So need to check before passing it to the database like below
Decimal Value1;
 if(TextBox1.text=="")
  {
   Value1 = 0.0;
   }
  else
   {
   Value1 = Convert.ToDecimal(TextBox1.text);
   }

  string Value2=  TextBox2.Text;


 Con.Open();
  Cmd = new SqlCommand("Insert into t1 values (@Value1,@Value2)", Con);


  Cmd.Parameters.AddWithValue("@Value1", Value1);
  Cmd.Parameters.AddWithValue("@Value2", Value2);
  Cmd.ExecuteNonQuery();
  Con.Close();


你必须确保你的TextBox1。 Text是一个数值,否则当您将空字符串作为数字传递时,您的SQL语句会给您一个错误。
You have to make sure your TextBox1.Text is a numeric value otherwise your SQL statement will give you an error as you are passing an empty string as a number.


这篇关于','附近的语法不正确。数据在数字字段中插入问题。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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