c# - ASP.Net 表单抛出 SQL 异常

标签 c# sql asp.net

当尝试在本地 SQL 数据库中保存和存储数据时,抛出 SQL 异常,

"A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: Named Pipes Provider, error: 40 - Could not open a connection to SQL Server)"

最初我认为我建立新数据连接的方式有问题。创建新表3次后,问题依旧。然后我尝试重新安装 Visual Studio 的 SQL 模块,它们也没有产生 +ve 结果。

无法从表中读取/存储任何数据。请帮忙。

public partial class signup : System.Web.UI.Page
{
    SqlConnection conn = new SqlConnection("Data Source=(localdb)\v11.0;Initial Catalog=KidzQuiz;Integrated Security=True");
    SqlCommand cmd;
    SqlDataAdapter da;
    DataSet ds;


protected void signupClick(object s, EventArgs e)
{
    //if (checkDuplicate() != 0)
        //signup_error.Text = "User already exists";
    //else if (checkEmpty() != 1)
       // signup_error.Text = "One or more fields is missing";
    //else
    //{
        string sqlcmd = "insert into User (name, email, school, city, standard, section) values (@n, @em, @sc, @city, @st, @sec)";
        cmd = new SqlCommand(sqlcmd, conn);
        cmd.Parameters.AddWithValue("@n", name.Text);
        cmd.Parameters.AddWithValue("@em", email_signup.Text);
        cmd.Parameters.AddWithValue("@sc", school.Text);
        cmd.Parameters.AddWithValue("@city", city.Text);
        cmd.Parameters.AddWithValue("@st", standard.SelectedItem.ToString());
        cmd.Parameters.AddWithValue("@sec", section.SelectedItem.ToString());
        conn.Open();
        cmd.ExecuteNonQuery();
        conn.Close();
    //}
}

最佳答案

在 LocalDB 本地数据库中,您还必须提供数据库文件的路径。

Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\db.mdf;Integrated Security=True

|数据目录|代表应用程序的App_Data文件夹

关于c# - ASP.Net 表单抛出 SQL 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31586171/

相关文章:

c# - 为什么我不能有一个不安全的 volatile 指针,只能在 AdHoc 模式下?

c# - 通过作为字符串传递的名称引用 c# 对象字段

c# - 如何使用 cwbx.dll 和 AS400System 类从 C# 到 IBM i (iSeries) 设置库列表

python - 是否可以将约束 unique = True 除了空字符串设置到 SQLAlchemy 中?

不同数据库上的 SQL 删除索引

asp.net - 在RedisSessionStateProvider 1.6中设置 session 超时

php - 如果已发送部分响应(分块),如何将浏览器发送到错误页面

c# - .NET 事件 - 阻止订阅者订阅事件

java - JPA,将复杂数据类型放在同一个表的实体内

css - Asp.Net 下拉列表和标签不显示内联