{string pn = ConfigurationManager.ConnectionStrings[DataNameInConfigfile].ProviderName//读取配置文件
string cs = ConfigurationManager.ConnectionStrings[DataNameInConfigfile].ConnectionString
//下面判断数据库类型并创建相应的对象
if (pn.ToUpper().Contains("OLEDB"))
{OleDbDataAdapter oledbda = new OleDbDataAdapter()
oledbda.SelectCommand = new OleDbCommand()
oledbda.SelectCommand.Connection = new OleDbConnection(cs)
return new DataBase(oledbda)}
if (pn.ToUpper().Contains("SQL"))
{SqlDataAdapter sqlda = new SqlDataAdapter()
sqlda.SelectCommand = new SqlCommand()
sqlda.SelectCommand.Connection = new SqlConnection(cs)
return new DataBase(sqlda)}
if (pn.ToUpper().Contains("ODBC"))
{OdbcDataAdapter odbcda = new OdbcDataAdapter()
odbcda.SelectCommand = new OdbcCommand()
odbcda.SelectCommand.Connection = new OdbcConnection(cs)
return new DataBase(odbcda)}
return null}
public static DataBase CreateData(string ConnectionString, string ProviderName)
{//以下判断数据库类型并创建相应的对象
if (ProviderName.ToUpper().Contains("OLEDB"))
{OleDbDataAdapter oledbda = new OleDbDataAdapter()
oledbda.SelectCommand = new OleDbCommand()
oledbda.SelectCommand.Connection = new OleDbConnection(ConnectionString)
return new DataBase(oledbda)}
if (ProviderName.ToUpper().Contains("SQL"))
{SqlDataAdapter sqlda = new SqlDataAdapter()
sqlda.SelectCommand = new SqlCommand()
sqlda.SelectCommand.Connection = new SqlConnection(ConnectionString)
return new DataBase(sqlda)
}if (ProviderName.ToUpper().Contains("ODBC"))
{OdbcDataAdapter odbcda = new OdbcDataAdapter()
odbcda.SelectCommand = new OdbcCommand()
odbcda.SelectCommand.Connection = new OdbcConnection(ConnectionString)
return new DataBase(odbcda)
}return null}}
答案补充
connectionStrings
add name="myData" connectionString="Provider=Microsoft.Jet.OLEDB.4.0Data Source=|DataDirectory|\WebData.aspx" providerName="oledb"/
配置文件,上边你自己改下,因为系统只能输入500个字符只能那样发给你了
add name="mySql" connectionString="Data Source=administrator\SQL2005Initial Catalog=GustBookPersist Security Info=TrueUser ID=saPassword=584077432" providerName="System.Data.SqlClient"/
/connectionStrings
没有问题啊System.Configuration.ConfigurationManager.appSettings["constring"].ToString()这个可以得到value的值啊也许是你的value连接字符串除了问题啦
value="server=服务器地址uid=数据库用户名pwd=密码database=数据库名"
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)