数据库问题-插入命令 [英] Problem with database - Insert command

查看:96
本文介绍了数据库问题-插入命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我正在为Windows Mobile编程应用程序,我只是初学者,并且我对INSERT命令有问题.这是我的资料.

Hi,

I am programming app for windows mobile, I am just begginer and I have problem with INSERT command. This is my source.

try
   {
       conn = new SqlCeConnection("Data Source = Super.sdf; password = ********");
       SqlCeCommand cmd = conn.CreateCommand();
       conn.Open();
       cmd = conn.CreateCommand();
       cmd.CommandText = "INSERT INTO tbl_jobs (id, company, tenant, number, address1, address2, postcode, town, van) VALUES (''"+textBox1.Text+"'',''"+comboBox1.Text+"'',''"+textBox13.Text+"'',''"+textBox12.Text+"'',''"+textBox11.Text+"'',''"+textBox10.Text+"'',''"+textBox9.Text+"'',''"+textBox8.Text+"'',''"+textBox7.Text+"'')";
       cmd.ExecuteNonQuery();
       conn.Close();
   }
   catch (SqlCeException ex)
   {
   }



我的问题是,INSERT命令所在的行有什么问题?现在可以正常工作,但是当我想添加另一个项目时,例如jobtype和,combobox2.text INSERT命令不起作用. INSERT命令中可以有多少个字符?对不起,我的英语不太好:)

谢谢您的帮助

Patrik



my question is, what is wrong with line where is INSERT command? Now is working properly, but when I want to add another item, for example jobtype and ,combobox2.text INSERT command doesn''t work. How many characters can be in command INSERT? I am sorry, my english is not very good :)

thank you for your help

Patrik

推荐答案

您的查询正确无误,但我没有发现任何错误.

同样,当您包含JobType和Combobox.Text时,也不会出现任何错误.

但是我怀疑JobType可能是数字类型(从我可以猜到的名称),而另一方面是Combobox.Text(来自看来是字符串)

你有尝试过吗?
Well your query is all correct and I am not finding any error.

Also when you will include JobType and Combobox.Text there will not be any error.

But I am suspecting that JobType may be a type of numeric(From the name I can guess) and and the other side Combobox.Text(From It seems that it''s a string)

Have you tried that ? is there a problem with ?


亲爱的朋友,
有问题吗? 您的代码似乎还可以,但是还不够完善.
错误原因可能是由于键约束(违反主键或任何其他约束)引起的.
提示->与让用户输入ID相比,我认为最好由应用程序生成密钥,并将其保存到数据库后,将ID值显示给用户

更好的编程习惯->
建议Alwasys使用sqlparam而不是直接用作字符串

例如
Dear Friend,
Your code seems ok but its not that much perfect.
The Cause Of error might be becaue of key constraint(primary key violation or any other constraints).
Tip->Instead of letting the user to enter the id, I think it would be better to generate a key by the application and after saving it to the database the id value is shown to the user

Better progamming practice->
It is alwasys advised to use sqlparam instead of using as string directly

Eg
SqlConnection con=new SqlConnection("....");
            con.Open();
            SqlCommand cmd = new SqlCommand("Insert into std values(@id,@name)", con);
            cmd.Parameters.Add("@id", SqlDbType.VarChar, 10);// second parameter specifies the data type of the value, third  is only used in case of strings which is the size/length of the field in database
            cmd.Parameters["@id"].Value = "ID01";//your value goes here
            cmd.Parameters.Add("@name", SqlDbType.VarChar, 10);
            cmd.Parameters["@name"].Value = "Name";//your value goes here
            cmd.ExecuteNonQuery()//to execute the command, this will return the number of rows affected by issuing the DML(insert/update/delete) statement
            con.Close();




使用sql参数的优势在于,当插入包含''的文本时,可能会导致错误,因此始终建议使用参数


最好使用StringBuilder,因为它有助于创建动态字符串,并且与字符串相比还可以提高高性能.

使用StringBuilder对象并将命令存储在其中,然后将其作为StingBuilderObject.toString()传递给命令对象的构造函数 随时联系/重播
希望您的疑虑现在已经清除

问候
Vipin Kumar Mallaya
程序员




Advantage of using sql parameters is that when inserting text containing '' it may cause error so it is always advised to use parameters


It would be better to use StringBuilder as it helps for creating dynamic strings and also delevers high performence compared to string.
ie
use a StringBuilder object and store the command in it then pass it to the command object''s constructor as StingBuilderObject.toString()
Feel free to contact/replay
I hope your doubt is cleared now

Regards
Vipin Kumar Mallaya
Programmer


使用
comboBox1.SelectedItem.ToString ();



代替



instead of

comboBox1.Text


这篇关于数据库问题-插入命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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