当我运行代码时,它将运行。出现登录和注册表格。但是在单击注册按钮后输入详细信息后,它会显示错误
“ system.data.dll中发生类型'system.data.sqlclient.sqlexception'的未处理异常” 并突出显示 con 和 con.open()
下面给出了编码
命名空间MovieBookingSystemCSharp { 公共部分课程注册:表格 { 公共注册 { InitializeComponent(); }
private void button1_Click(object sender, EventArgs e)
{
SqlConnection **con** = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\dell\Documents\Visual Studio 2015\Projects\MovieBookingSystemCSharp\MovieBookingSystemCSharp\movie.mdf;Integrated Security=True");
**con.Open();**
try
{
string str = "INSERT INTO user1(name,mobile,email,pass) VALUES('" + textBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "','" + TextBox4.Text + "'); ";
SqlCommand cmd = new SqlCommand(str, con);
cmd.ExecuteNonQuery();
//-------------------------------------------//
string str1 = "select max(Id) from user1;";
SqlCommand cmd1 = new SqlCommand(str1, con);
SqlDataReader dr = cmd1.ExecuteReader();
if (dr.Read())
{
MessageBox.Show("New User Registered Successfully..");
Form1 obj = new Form1();
obj.ShowDialog();
this.Hide();
}
}
catch (SqlException excep)
{
MessageBox.Show(excep.Message);
}
con.Close();
}
private void button2_Click(object sender, EventArgs e)
{
textBox1.Text = "";
TextBox2.Text = "";
TextBox3.Text = "";
TextBox4.Text = "";
}
}
}
答案 0 :(得分:0)
尝试修改“ AttachDbFilename”属性,如下所示,该路径中没有空格。
SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\testuser1\Documents\vs2015\Projects\MovieBookingSystemCSharp\MovieBookingSystemCSharp\movie.mdf;Integrated Security=True");