一般都是直接用某個Connection去建立連接
像是SqlConnection或是OleDbConnection..等等
但另外有個通用法,只要改Provider就可改連接的方法
這是在 System.Data.Common裡面的
DbProviderFactory theFactory =
DbProviderFactories.GetFactory("System.Data.SqlClient");
ConnectionStringSettings theConnectionString =
ConfigurationManager.ConnectionStrings["myConnectnioStr"];
DbConnection theConnection = theFactory.CreateConnection();
theConnection.Open();
if (theConnection.State == System.Data.ConnectionState.Open)
returnMessage = "資料庫的連接已開";
}
catch (DbException dbexception)
{
returnMessage = dbexception.Message;
}
catch (Exception exception)
{
returnMessage = exception.Message;
}
另外補上theFactory可用的方法和屬性
方法
CreateCommand
CreateCommandBuilder
CreateConnection
CreateConnectionStringBuilder
CreateDataAdapter
CreateDataSourceEnumerator
CreateParameter
CreatePermission
屬性
CanCreateDataSourceEnumerator
留言列表