问题描述
我正在为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
推荐答案
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
这篇关于数据库问题-插入命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!