'system.data.sqlclient.sqlconnection'的类型初始值设定项引发了异常。 [英] The type initializer for 'system.data.sqlclient.sqlconnection' threw an exception.

查看:526
本文介绍了'system.data.sqlclient.sqlconnection'的类型初始值设定项引发了异常。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

?xml version =1.0encoding =utf;

< configuration>

< startup>

< supportedruntime version =v4.0sku =。NETFramework,Version = v4.5>



< configsections>





< connectionstring>

< add name =ConnectionStringprovidername =System.Data .SqlClientconnectionstring =server = CTSINTBMC; database = One995; Integrated Security = SSPI; Persist Security Info = False; Max pool size = 20; Min pool size = 0>







控制台应用程序代码如下



SqlConnection sqlConnection = new SqlConnection( ConnectionString);

SqlCommand cmd = new SqlCommand();

SqlDataReader reader;

cmd.commandtext =select * from customers;

cmd.CommandType = CommandType.Text;

cmd.Connection = sqlConnection;

sqlConnection.Open();

reader = cmd.ExecuteReader();

sqlConnection.Close();





当我执行上面的代码时显示错误如下



'系统的类型初始值设定项。 Data.SqlClient.SqlConnection'引发异常。



以下错误显示如下以下



SqlConnection sqlConnection = new SqlConnection(ConnectionString);



请帮我解决上面代码中的问题



我尝试过:



?xml version =1.0encoding =utf;

< configuration>

< startup>

< supportedruntime version =v4.0sku =。NETFramework,Version = v4.5 >



< configsections>





< connectionstring>

< add name =ConnectionStringp rovidername =System.Data.SqlClientconnectionstring =server = CTSINTBMC; database = One995; Integrated Security = SSPI; Persist Security Info = False; Max pool size = 20; Min pool size = 0>







控制台应用程序代码如下



SqlConnection sqlConnection = new SqlConnection(ConnectionString);

SqlCommand cmd = new SqlCommand();

SqlDataReader reader;

cmd.commandtext = select * from customers;

cmd.CommandType = CommandType.Text;

cmd.Connection = sqlConnection;

sqlConnection.Open();

reader = cmd.ExecuteReader();

sqlConnection.Close();





当我执行上面的代码时显示如下错误



'System.Data.SqlClient.SqlConnection'的类型初始化程序引发异常。



出现以下错误在下面的行显示如下



SqlConnection sqlConnection = new SqlConnection(ConnectionString);



please帮助我上面的代码有什么问题

?xml version="1.0"encoding="utf";
<configuration>
<startup>
<supportedruntime version="v4.0" sku=".NETFramework,Version=v4.5">

<configsections>



<connectionstring>
<add name="ConnectionString" providername="System.Data.SqlClient" connectionstring="server=CTSINTBMC;database=One995;Integrated Security=SSPI;Persist Security Info=False;Max pool size = 20;Min pool size = 0">



in console application code as follows

SqlConnection sqlConnection = new SqlConnection("ConnectionString");
SqlCommand cmd = new SqlCommand();
SqlDataReader reader;
cmd.commandtext = "select * from customers";
cmd.CommandType = CommandType.Text;
cmd.Connection = sqlConnection;
sqlConnection.Open();
reader = cmd.ExecuteReader();
sqlConnection.Close();


when i execute the above code shows error as follows

The type initializer for 'System.Data.SqlClient.SqlConnection' threw an exception.

the following error shows in below line as follows

SqlConnection sqlConnection = new SqlConnection("ConnectionString");

please help me what is the problem in my above code

What I have tried:

?xml version="1.0"encoding="utf";
<configuration>
<startup>
<supportedruntime version="v4.0" sku=".NETFramework,Version=v4.5">

<configsections>


<connectionstring>
<add name="ConnectionString" providername="System.Data.SqlClient" connectionstring="server=CTSINTBMC;database=One995;Integrated Security=SSPI;Persist Security Info=False;Max pool size = 20;Min pool size = 0">



in console application code as follows

SqlConnection sqlConnection = new SqlConnection("ConnectionString");
SqlCommand cmd = new SqlCommand();
SqlDataReader reader;
cmd.commandtext = "select * from customers";
cmd.CommandType = CommandType.Text;
cmd.Connection = sqlConnection;
sqlConnection.Open();
reader = cmd.ExecuteReader();
sqlConnection.Close();


when i execute the above code shows error as follows

The type initializer for 'System.Data.SqlClient.SqlConnection' threw an exception.

the following error shows in below line as follows

SqlConnection sqlConnection = new SqlConnection("ConnectionString");

please help me what is the problem in my above code

推荐答案

hey it may be the problem with app.config settings.

u hv to code like this

 

<appSettings>

<add key ="ConnectionString" value="Data Source=server name;Initial Catalog=db name;User ID= sa;Password= sa"/>

</appSettings>

 

if u code this wrongly ,typeintializer error w'll throw.

Also if the key name is used different from app.config also exception will throw.


这篇关于'system.data.sqlclient.sqlconnection'的类型初始值设定项引发了异常。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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