在web.config中使用SQL2005是这样使用的:
web.config:
<connectionStrings>
<add name="win2009ConnectionString" connectionString="Data Source=COMPUTER;Initial Catalog=win2010;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>
调用:
public SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["win2009ConnectionString"].ConnectionString);
在winform项目中:
app.config:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<configSections>
</configSections>
<connectionStrings>
<add name="hj009_consumeConnectionString"
connectionString="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\hj009_consume.mdb"
providerName="System.Data.OleDb" />
</connectionStrings>
</configuration>
求教:怎么调用winform 中的ACCess的连接,先谢谢!
ADO.NET
连接到 ODBC 数据源
ODBC .NET Framework 数据提供程序通过 OdbcConnection 对象提供与使用 ODBC 公开的数据源的连接。
using (OdbcConnection connection = new OdbcConnection(connectionString))
{
connection.Open();
// Do work here.
}
以下是连接实例:
ACCESS
string conStr=@"provider=microsoft.Jet.OleDb.4.0;data source=d:\\accessData.mdb;uid=sa;pwd=dd";
这是从常用的一个操作Access数据库的SQLHelper类中提取的连接方式和执行数据库操作一个例子,希望对你有些帮助:
1 DbProviderFactory provider;
2 protected static string connectionString = ConfigurationManager.ConnectionStrings["Connection"].ConnectionString;
3 /// <summary>
4 /// 默认构造函数;
5 /// </summary>
6 public SqlHelper()
7 {
8 provider = DbProviderFactories.GetFactory(ConfigurationManager.ConnectionStrings["Connection"].ProviderName);
9 }
10 #region 执行简单SQL语句
11 /// <summary>
12 /// 执行SQL语句,返回影响的记录数
13 /// </summary>
14 /// <param name="SQLString">SQL语句</param>
15 /// <returns>影响的记录数</returns>
16 public int ExecuteSql(string SQLString)
17 {
18 using (IDbConnection connection = provider.CreateConnection())
19 {
20 connection.ConnectionString = connectionString;
21 using (IDbCommand cmd = provider.CreateCommand())
22 {
23 cmd.Connection = connection;
24 cmd.CommandText = SQLString;
25 try
26 {
27 connection.Open();
28 int rows = cmd.ExecuteNonQuery();
29 return rows;
30 }
31 catch (DbException ex)
32 {
33 connection.Close();
34 connection.Dispose();
35 throw ex;
36 }
37 }
38 }
39 }
其中DbProviderFactory的命名空间为System.Data.Common
怎么都这些蛋疼问题啊?不说了,飘过