为什么数据不进入数据库表 [英] Why data is not going to data base table
本文介绍了为什么数据不进入数据库表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
private void button1_Click(object sender, EventArgs e)
{
string connetionString = null;
SqlConnection cnn ;
connetionString = "Data Source=JDPANDEY-PC;Initial Catalog=Janardan;User ID = sa;Password= 123";
cnn = new SqlConnection(connetionString);
try
{
cnn.Open();
MessageBox.Show ("Connection Open ! ");
string insertString = @" insert into emp values ('" + textBox1.Text.Trim () + "','" + textBox2.Text .Trim() + "','" + textBox3.Text.Trim() + "','"+ textBox4.Text.Trim() +"')";
cnn.Close();
}
catch (Exception ex)
{
MessageBox.Show("Can not open connection ! ");
}
推荐答案
因为您只是在字符串变量中分配了INSERT
查询,而从未使用过.
-Eduard
Because you just assigned yourINSERT
query in a string variable and never used it.
-Eduard
您还应该使用以下代码行
You shoul also use these lines of code
SqlCommand cmd = new SqlCommand(insertstring, cnn);
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
cnn.Close();
string insertString =" insert into emp values ('" + textBox1.Text.Trim () + "','" + textBox2.Text .Trim() + "','"+ textBox3.Text.Trim() + "', '" + textBox4.Text.Trim() + "')"
SqlCommand ScalarCommand = new SqlCommand();
try
{
ScalarCommand.CommandText = insertString;
ScalarCommand.Connection = Cnn;
ScalarCommand.ExecuteScalar();
使用上面的代码.
确保数据库的所有数据类型都与插入值匹配.
use above code.
make sure all datatype of database is matches with insert value.
这篇关于为什么数据不进入数据库表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文