【发布时间】:2017-05-17 17:28:48
【问题描述】:
这是我正在处理的代码;
public partial class Form2 : Form
{
SqlConnection sc = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True");
SqlDataAdapter sda;
SqlCommand command;
SqlCommand commands;
public Form2()
{
InitializeComponent();
}
private void button2_Click(object sender, EventArgs e)
{
this.Close();
Form1 f1 = new Form1();
f1.Show();
}
private void button1_Click(object sender, EventArgs e)
{
command = new SqlCommand(@"SELECT * FROM [Table] WHERE email='" + textBox4.Text + "'", sc);
sda = new SqlDataAdapter(command);
DataSet ds = new DataSet();
sda.Fill(ds);
int i = ds.Tables[0].Rows.Count;
if (i == 1)
MessageBox.Show("Email Already Taken");
else
{
sc.Open();
command = new SqlCommand("INSERT INTO [Table](name,surname,yearofbirth,adress_home_city,adress_home_block,adress_home_street,adress_work_city,adress_work_block,adress_work_street,email,password) VALUES('"+textBox1.Text+"','"+textBox2.Text+ "','" + textBox3.Text + "','" + textBox4.Text + "','" + textBox5.Text + "','" + textBox7.Text + "','" + textBox10.Text + "','" + textBox11.Text + "','" + textBox8.Text + "','" + textBox9.Text + "','" + textBox6.Text + "') ", sc);
command.ExecuteNonQuery();
sc.Close();
MessageBox.Show("Success");
}
当我调试它时它可以工作,它是一个注册表单,我可以使用我在此表单中提供的信息登录。
但是当关闭时,这个插入命令并没有保存在我的数据库中。
// 附加信息 ;
在注册表格时,我会收到成功消息,如果我用相同的信息再次尝试,我会收到电子邮件已被接收的消息
【问题讨论】:
-
我希望您检查的是正确的数据库, "Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True" ,这不是数据库直接从管理工作室打开。
-
使用参数避免SQL注入和格式错误。你真的有一个名为“表”的表吗?如果成功,ExecuteNonQuery 将返回受影响的行数。您可能正在查看数据库的副本——查看您的连接属性设置。
-
它是如何工作但不保存的?这不是行不通吗?
-
我确实将我的表命名为“表”:D 并且它是正确的数据库。我在我的数据库中做了一些测试行,它们也在我的登录屏幕上工作。
-
AttachDbFilename=|DataDirectory|\Database1.mdf;是您需要附加到 sql server management studio 以检查表中的值的实际数据库名称,或者您可以将数据库源更改为已附加到 SSMS 的某个数据库
标签: c# sql executenonquery