数据库没有连接。我的代码中有任何错误 [英] data base is not connected .is there any mistake in my code

查看:73
本文介绍了数据库没有连接。我的代码中有任何错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 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 =;
}

解决方案

看看这里: ASP.NET Web应用程序的SQL Server连接字符串 [ ^ ],选择您的MS SQL Server版本并检查连接字符串是否正确集。

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 = "";
    }

解决方案

Have a look here: SQL Server Connection Strings for ASP.NET Web Applications[^], choose your version of MS SQL Server and check if the connection string is properly set.


这篇关于数据库没有连接。我的代码中有任何错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆