dridview没有显示任何数据 [英] dridview not displaying any data
问题描述
SqlConnection objCon = new SqlConnection(@Data Source = MIS-PC; Initial Catalog = projecthotel; Integrated Security = True);
SqlCommand objCmd = new SqlCommand();
objCmd.Connection = objCon;
string query =insert into Reservation values('+ textBox1.Text + ','+ comboBox2.Text +','+ textBox4.Text +','+ textBox6.Text +','+ textBox7.Text +','+ textBox15.Text + ','+ comboBox7.Text +','+ comboBox8.Text +','+ comboBox9.Text +','+ textBox25.Text +','+ textBox17.Text + ','+ textBox18.Text +','+ textBox20.Text +','+ textBox21.Text +');
objCmd.CommandText = query;
objCon.Open();
int j = objCmd.ExecuteNonQuery();
if(j> 0)
{
MessageBox.Show(Record Inserted,www.codingresolved.com);
dataGridView3.Rows.Clear();
}
objCon.Close();
Form1 form1 = new Form1();
form1.Show();
}
SqlConnection objCon = new SqlConnection(@"Data Source=MIS-PC;Initial Catalog=projecthotel;Integrated Security=True");
SqlCommand objCmd = new SqlCommand();
objCmd.Connection = objCon;
string query = "insert into Reservation values('" + textBox1.Text + "', '" + comboBox2.Text + "','" + textBox4.Text + "','" + textBox6.Text + "','" + textBox7.Text + "','" + textBox15.Text + "','" + comboBox7.Text + "','" + comboBox8.Text + "','" + comboBox9.Text + "','" + textBox25.Text + "','" + textBox17.Text + "','" + textBox18.Text + "','" + textBox20.Text + "','" + textBox21.Text + "')";
objCmd.CommandText = query;
objCon.Open();
int j = objCmd.ExecuteNonQuery();
if (j > 0)
{
MessageBox.Show("Record Inserted","www.codingresolved.com");
dataGridView3.Rows.Clear();
}
objCon.Close();
Form1 form1 = new Form1();
form1.Show();
}
推荐答案
对于初学者,不要那样做!不要连接字符串以构建SQL命令。它让您对意外或故意的SQL注入攻击持开放态度,这可能会破坏您的整个数据库。请改用参数化查询。
其次,INSERT操作会影响数据库,但不会影响您的应用程序显示。
第三,当你成功插入记录时,你正在清空DataGridView,所以它总是为空。
你的代码中没有任何内容显示记录,除非您的新表单访问数据库并检索它们。
For starters, don't do it like that! Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.
Secondly, an INSERT operation affect the DB but it doesn't affect your application display.
Thirdly, you are emptying the DataGridView when you have successfully inserted the record, so it will always be empty.
There is nothing in your code that displays records, unless your new form access the DB and retrieves them.
这篇关于dridview没有显示任何数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!