你可以尝试在config文件中写入如下代码:Provider=MySQLProv;Data Source=your database;User Id=your user name;Password=your password;其中MySQLProv是OLEDB链接方式所需要的驱动,你要确保驱动安装好后就能链接MySQL数据库了,因为SQL Server是不需要另外安装驱动的(你安装SQL是已经默认安装),这是有点不同的地方。访问前要先下载两个组件:odbcnet和MYSQL的ODBC驱动(MySQL Connector/ODBC (MyODBC) driver)
访问数据库有两种方式:
Windows身份验证登录(不需要用户名和密码,适用于访问本地数据库,应用程序和SQL软件安装在同一台服务器)
SQL Server 身份验证登录(适用于访问其他计算机的数据库,当然也可以用这种方式访问本地数据库)
根据楼主的情况应该用 SQL Server 身份验证登录
数据库连接字符串一般写在 connectionStrings节点下,非强制性(如写在appsettings节点下),只不过这样可以用
ConfigurationManagerConnectionStrings["connString"]ConnectionString;
方便访问
以下是笔者的一个 webconfig 文件的内容
<xml version="10" encoding="utf-8" >
<configuration>
<connectionStrings>
<add name="connString" connectionString="Data Source=;Initial Catalog=TestDatabase;Integrated Security=SSPI"/>
</connectionStrings>
</configuration>
附:数据库连接字符串的写法
SQL Server connection strings
SQL ODBC connection strings
Standard Security:< br> "Driver={SQLServer};Server=Your_Server_Name;Database=Your_Database_Name;Uid=Your_Username;Pwd=Your_Password;"
Trusted connection:< br> "Driver={SQLServer};Server=Your_Server_Name;Database=Your_Database_Name;Trusted_Connection=yes;"
SQL OLE DB connection strings
Standard Security:
"Provider=SQLOLEDB;Data Source=Your_Server_Name;Initial Catalog= Your_Database_Name;UserId=Your_Username;Password=Your_Password;"
Trusted connection:
"Provider=SQLOLEDB;Data Source=Your_Server_Name;Initial Catalog=Your_Database_Name;Integrated Security=SSPI;"
SQL OleDbConnection NET strings
Standard Security:
"Provider=SQLOLEDB;Data Source=Your_Server_Name;Initial Catalog= Your_Database_Name;UserId=Your_Username;Password=Your_Password;"
Trusted connection:
"Provider=SQLOLEDB;Data Source=Your_Server_Name;Initial Catalog=Your_Database_Name;Integrated Security=SSPI;"
SQL SqlConnection NET strings
Standard Security:
1 "Data Source=Your_Server_Name;Initial Catalog= Your_Database_Name;UserId=Your_Username;Password=Your_Password;" < br>2 "Server=Your_Server_Name;Database=Your_Database_Name;UserID=Your_Username;Password=Your_Password;Trusted_Connection=False"
Trusted connection:
1 "Data Source=Your_Server_Name;Initial Catalog=Your_Database_Name;Integrated Security=SSPI;"
2"Server=Your_Server_Name;Database=Your_Database_Name;Trusted_Connection=True;"
希望回答对你有帮助
最好是用带用户和密码的。
<add
name="connstr"
connectionstring="data
source=microsof-e46d5f\sql2005;initial
persist
security
info=true;uid=登陆帐户;pwd=密码;catalog=数据库名;database=数据库名、这俩个用哪个都可以,如果一起用系统会选择database的"
providername="systemdatasqlclient"
/>
<connectionStrings>
<add name="TestConnectionString" connectionString="Provider=MicrosoftJetOledb40;Server=(local);Database=test;uid=root;pwd=123456;port=3306" providerName="SystemDataOleDb"/>
</connectionStrings>
<add name="MySql" connectionString="server=100529;user id=root; password=wrs123; database=ultrax; pooling=false;charset=gb2312;allow zero datetime=true" />
方法/步骤
新建一个网站,打开webconfig文件,在connectionString配置节点添加add节点进行数据库进行数据库连接配置代码如下:
<connectionStrings>
<add name="SqlConnection" providerName="SystemDataSqlClient"
connectionString="server=;uid=sa;pwd=accp;database=pubs"/>
</connectionStrings>
在设计器中双击Defaultaspx页面空白处,并 Page_load事件处理函数中,添加访问配置连接字符串代码:
protected void Page_load(object sender,EventArgs e)
{
string name="SqlConnection";
string connectionString=ConfigurationManagerConnectionStrings
["SqlConnection"]ConnectionString;
string providerName=ConfigurationManagerConnerctionStrings
string providerName=ConfigurationManagerConnerctionStrings
["SqlConnection"]ProviderName;
StringBuilder builder=new StringBuilder(stringEmpty);
builderAppendFormat("<b>连接字符串键名:{0}</b><br/>",name);
builderAppendFormat("<b>数据库连接字符串:{0}</b><br/>",connectionString);
builderAppendFormat("<b>数据t提供程序名称:{0}</b><br/>,providerName");
thisResponseWrite(builderToString());
}
3
运行网站,在Defaultaspx页面输出了配置完成连接字符串
以上就是关于C#远程连接MySQL,配置文件Web.config如何写连接字符串全部的内容,包括:C#远程连接MySQL,配置文件Web.config如何写连接字符串、C# web Web.config里sql连接数据库、asp.net中如何配置web.config文件链接SQL数据库等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)