移动应用程序,insert sql语句将插入多行!!!! [英] Mobile App,the insert sql statement inserts more then one row!!!!
本文介绍了移动应用程序,insert sql语句将插入多行!!!!的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我正在开发一个移动应用程序,当按下回车键时,我必须从txtbox到数据库表中插入一些值.但是sql命令会插入多行,直到我向txt框添加新值为止.问题出在connection.Close();我应该在哪里关闭它,或者如果需要检查该怎么做?请帮忙!
提前谢谢.
这是部分代码:
hello all,
I am developing a mobile app and i have to insert some values from the txtbox to dhe database table when enter key is pressed.but the sql command inserts more then one row,until i add a new value to the txt box.i think the problem is at the connection.Close();where should i close it,or should i make any if checks??Please help!
Thanks in advance.
Here some part of code:
string _connString = String.Format(System.Globalization.CultureInfo.InvariantCulture,
@"Data Source = {0}\SQL_CE.sdf", Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase));
SqlCeConnection _connection = new SqlCeConnection(_connString);
if (e.KeyChar == (char)13)
{
_connection.Open();
SqlCeCommand desc_command = new SqlCeCommand("select description from Items where barcode=@barcode;", _connection);
desc_command.Parameters.AddWithValue("@barcode", txt_barcode.Text);
SqlCeDataReader rdr = desc_command.ExecuteReader();
if (rdr.Read())
{
txt_description.Text = rdr.GetString(0);
}
else
{
txt_description.Text = "-----";
}
SqlCeCommand _addcommand = new SqlCeCommand("insert into Inventory values(@barcode,@amount);", _connection);
_addcommand.Parameters.AddWithValue("@barcode", txt_barcode.Text);
_addcommand.Parameters.AddWithValue("@amount", txt_amount.Text);
_addcommand.ExecuteNonQuery();
txt_barcode.Text = "";
_connection.Close();
};
推荐答案
我解决了,还是谢谢.我只是使用了while循环,而txtbox!=",打开了连接,插入了语句,然后关闭连接!!
I resolved it,Thanks anyway.I just used a while loop,while the txtbox != "" ,open the connection,the insert statement,then close the connection!!
这篇关于移动应用程序,insert sql语句将插入多行!!!!的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文