问题在网络配置文件的连接字符串 [英] Issue with connection string in web config file

查看:138
本文介绍了问题在网络配置文件的连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想设置网络配置文件中的MySQL连接。

I am trying to set the mysql connection in web config file.

下面是连接字符串代码:

Here is the connection string code:

    <connectionStrings>
    <add name="connstring" 
         connectionString="DRIVER={MySQL ODBC= 3.51=        Driver};Database=marctest;Server=localhost;UID=root;PWD=1234;" 
         providerName="System.Data.SqlClient"/>
    </connectionStrings>

和我正在访问它在接下来的步骤:

and i am accessing it in following step:

    MySqlConnection connmysql = new MySqlConnection(WebConfigurationManager.ConnectionStrings["connstring"].ConnectionString);

当我运行我的代码,它会生成一个空引用异常。对象引用未设置为实例一个对象。

When I run my code it generates a null reference exception."Object reference not set to an instance of an object."

我怎样才能解决这个问题?

How can I resolve this issue?

推荐答案

如果您使用的是那么MySQL的.Net连接器你的配置设置应类似于以下

If you are using the MySql .Net Connector then your configuration setting should look similar to the following

<add name="connstring" connectionString="server=localhost;User Id=root;Persist Security Info=True;database=marctest;password=PASSWORD" providerName="MySql.Data.MySqlClient"/>



你的代码应该看起来像。

Then your code should look like.

using (MySqlConnection dbConn = new MySqlConnection(ConfigurationManager.ConnectionStrings["connstring"].ConnectionString)) 
{ 
    // Database work done here 
}

这篇关于问题在网络配置文件的连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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