本文介绍了数据库没有连接。我的代码中有任何错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
protected void Button1_Click(object sender,EventArgs e)
{
SqlConnection conn = new SqlConnection(@Data Source = (LocalDB)\ v11.0; AttachDbFilename = E:\ ngani \websites\iconlabssite part2 \ App_Data \ maindatabase.mdf; Integrated Security = True;);
SqlCommand insert = new SqlCommand(insert into regform(username,password,mobileno,emailid)value(@ username,@ password,@ phoneno,@ emailid),conn);
insert.Parameters.AddWithValue(@ username,TextBox1.Text);
insert.Parameters.AddWithValue(@ password,TextBox2.Text);
insert.Parameters.AddWithValue(@ phoneno,TextBox4.Text);
insert.Parameters.AddWithValue(@ emailid,TextBox5.Text);
尝试
{
conn.Open();
insert.ExecuteNonQuery();
}
catch(Exception ex)
{
LMsg.ForeColor = System.Drawing.Color.Red;
LMsg.Text =保存在数据库时出错;
conn.Close();
}
TextBox1.Text =;
TextBox2.Text =;
TextBox3.Text =;
TextBox4.Text =;
TextBox5.Text =;
TextBox6.Text =;
}
解决方案
protected void Button1_Click(object sender, EventArgs e) { SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=E:\gnani\websites\iconlabssite part2\App_Data\maindatabase.mdf;Integrated Security=True;"); SqlCommand insert = new SqlCommand("insert into regform(username, password, mobileno,emailid) values(@username, @password, @phoneno,@emailid)", conn); insert.Parameters.AddWithValue("@username", TextBox1.Text); insert.Parameters.AddWithValue("@password", TextBox2.Text); insert.Parameters.AddWithValue("@phoneno", TextBox4.Text); insert.Parameters.AddWithValue("@emailid", TextBox5.Text); try { conn.Open(); insert.ExecuteNonQuery(); } catch (Exception ex) { LMsg.ForeColor = System.Drawing.Color.Red; LMsg.Text = "Error when saving on database"; conn.Close(); } TextBox1.Text = ""; TextBox2.Text = ""; TextBox3.Text = ""; TextBox4.Text = ""; TextBox5.Text = ""; TextBox6.Text = ""; }
解决方案
这篇关于数据库没有连接。我的代码中有任何错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!