如何解决“'ServerVersion'引发类型'System.InvalidOperationException'”的异常? [英] How to fix "'ServerVersion' threw an exception of type 'System.InvalidOperationException'"?
问题描述
我已经启动并运行了一个本地SQL Server数据库,并且我正在尝试以一种看似防故障的方式连接到它:
I have got a local SQL Server DB, up and running, and I am trying to connect to it in a seemingly failproof way:
new SqlConnection(@"Server=(localdb)\v12.0;Integrated Security=true;Database=MyDBName;");
但是,此行引发异常:'ServerVersion'引发了类型为'System.InvalidOperationException的异常'?我该如何解决?
However, this line throws an exception: "'ServerVersion' threw an exception of type 'System.InvalidOperationException'"? What could I do to fix it?
我已经运行
sqllocaldb create "v12.0"
但似乎没有什么区别。
推荐答案
步骤1:转到WebConfig文件并编写此代码:
Step 1: go to WebConfig File and Write this Code:
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 ; --->这是您的服务器名
Templete_2_DB; ---->这是您的数据库名称
SYED\SQLEXPRESS; ---> this is your servername Templete_2_DB; ----> this is your database name
步骤2:转到您的页面事件并编写如下代码。
step 2: Go to your page event and write code like this..
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");
}
在此处编码
继续...在这方面,您将得到指导
Go ahead... in this you will be guided
https://www.youtube.com/watch?v=Mo0ECWKVVDU
这篇关于如何解决“'ServerVersion'引发类型'System.InvalidOperationException'”的异常?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!