如何修复“‘ServerVersion’引发‘System.InvalidOperationException’类型的异常”? [复制]

2024-01-23

我有一个本地 SQL Server 数据库,已启动并正在运行,并且我正在尝试以一种看似万无一失的方式连接到它:

new SqlConnection(@"Server=(localdb)\v12.0;Integrated Security=true;Database=MyDBName;");

然而,这一行抛出了一个异常:“‘ServerVersion’抛出了‘System.InvalidOperationException’类型的异常”?我能做什么来解决它?

我跑了

sqllocaldb create "v12.0"

但这似乎没有什么区别。


第 1 步:转到 Web 配置文件并编写以下代码:

enter code here

 <connectionStrings>

 <add name ="MyDbConn" ---> write same it is 

     connectionString="Server=SYED\SQLEXPRESS; database=Templete_2_DB; 
     Trusted_Connection=True" providerName="System.data.sqlclient"
     />
  </connectionStrings>

代码在这里

SYED\SQLEXPRESS; ---> 这是你的服务器名称 模板_2_DB; ----> 这是你的数据库名称

第2步:转到您的页面事件并编写如下代码..

enter code here


 SqlConnection con = new SqlConnection(

 WebConfigurationManager.ConnectionStrings["MyDbConn"].ConnectionString);

 SqlCommand cmd = new SqlCommand("select * from Accounts_Data where 
 UserName=@username and Password=@password", con);
        cmd.Parameters.AddWithValue("@username", txt_username.Text);
        cmd.Parameters.AddWithValue("@password", txt_userPassword.Text);
        SqlDataAdapter sda = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        sda.Fill(dt);
        con.Open();
        int i = cmd.ExecuteNonQuery();
        con.Close();

        if (dt.Rows.Count > 0)
        {
            Response.Redirect("Default.aspx");
        }

代码在这里

继续...您将被引导

https://www.youtube.com/watch?v=Mo0ECWKVVDU https://www.youtube.com/watch?v=Mo0ECWKVVDU

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如何修复“‘ServerVersion’引发‘System.InvalidOperationException’类型的异常”? [复制] 的相关文章

随机推荐