【发布时间】:2017-07-15 11:38:32
【问题描述】:
编译器没有显示任何错误。我想知道它有什么问题。
是的,它也没有显示消息“插入!!”
这是我在 Winforms 中插入的第一个数据。是的,我是新人。
代码:
public partial class Phone : Form
{
SqlConnection con = new SqlConnection(@"Data Source=.;Initial Catalog=phonemo;Persist Security Info=True;User ID=sa;Password=***********");
public Phone()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
DialogResult dialogResult = MessageBox.Show("This will Clear Text", "New", MessageBoxButtons.YesNo);
if (dialogResult == DialogResult.Yes)
{
//do something
}
else if (dialogResult == DialogResult.No)
{
//do something else
}
textBox1.Text = "";
textBox2.Clear();
textBox3.Text = "";
textBox4.Clear();
comboBox1.SelectedIndex = -1;
textBox1.Focus();
con.Open();
String query = "INSERT INTO phonemoo (Fname,Sname,Mobile,Email,Catagory) VALUES ('"+ textBox1.Text +"','"+ textBox2.Text +"','"+ textBox3.Text +"','"+ textBox4.Text +"','"+ comboBox1.Text + "')";
SqlDataAdapter sda = new SqlDataAdapter(query,con);
sda.SelectCommand.ExecuteNonQuery();
con.Close();
MessageBox.Show("Inserted !!");
}
}
谢谢
【问题讨论】:
-
为什么要清除所有控件然后保存它们的(空)值?
-
清除什么??不明白..你能解释一下吗
-
停止一切,在阅读完所有有关 SQL 注入的知识之前不要编写任何代码。另外,不要以
sa连接。然后,您打开连接一次并在每次单击按钮时将其关闭 - 只需在每次单击按钮时打开它即可。最后,只需使用调试器查看发生了什么 -
@BurhanAhmed,在你的代码中你有:
textBox1.Text = "";textBox2.Clear(); ...然后你存储它们的值(在查询中) -
@LucasTrzesniewski 是的,我对 sql 注入知之甚少..但现在我只需要将我的数据插入数据库:P
标签: c# sql-server database winforms insert