在 appSettings 里配置字符串,不在 connectionStrings 里写连接数据库的配置
来源:互联网 发布:上海跳跃网络吧爆吧 编辑:程序博客网 时间:2024/06/14 01:55
一点点的不注意就不能成功。在web.config 里面写的连接数据库有两种,一种在<connectionStrings/>里面写,现在采用的比较多,另一种是在<appSettings/ >里写,都能实现,但是不同的连接方法也有细微的差别。今天一天不在状态,磨磨蹭蹭终于修改成功才把这个程序写成功,现在和大家分享一下,希望大家多多指教。
1.在web.config里面写如下。
<appSettings >
<add key="SQLConnectionString" value="Provider=sqloledb;data source=../SQLEXPRESS;initial catalog=Database1;User ID= sa;Password=xxxx ;persist security info=False;" />
</appSettings>
2.在对应页面的.cs文件里这样写,本例是利用
using System.Data;
using System.Data.OleDb;
using System.Data.SqlClient;
.......
//把文件导入到数据库
//string conn = ConfigurationSettings.AppSettings["SQLConnectionString"]; 已经过时所以用下面这句
string strconn = System.Configuration.ConfigurationManager.AppSettings["SQLConnectionString"];
OleDbConnection myConnection = new OleDbConnection(strconn); //一开始有问题,提示没有Provider=sqloledb;
string strSql1 = " if exists(select * from sysobjects where id = object_id('TempImportData1')) drop table TempImportData1 ";
string strSql2 = " select * into TempImportData1 from opendatasource( 'Microsoft.Jet.OLEDB.4.0','Data Source=/"" + strFilePathAll + "/";User ID=Admin;Password=;Extended properties=Excel 8.0')...[Sheet1$] ";
try
{
OleDbCommand comm1 = new OleDbCommand(strSql1, myConnection);
OleDbCommand comm2 = new OleDbCommand(strSql2, myConnection);
myConnection.Open();
comm1.ExecuteNonQuery();
comm2.ExecuteNonQuery();
myConnection.Close();
}
catch(OleDbException ex)
{
// Console.WriteLine("--------NO------------");
Response.Write(ex.Message );
}
finally
{
myConnection.Close();//自居都会确保强制关闭数据连接。
}
..............
总结:本人一开始怎么也没法把它连接,但是最终发现问题出现在没有Provider=sqloledb;其实在<connectionStrings/>配置时大都是这样写的
<connectionStrings>
<add name="MyTestConnectionString" connectionString="Data Source=OPEN-EQIANG/SQLEXPRESS;Initial Catalog=WebSiteTest;User ID=sa;Password=qwertyuiop;Persist Security Info=True;"
providerName="System.Data.SqlClient" />
</connectionStrings>
这样里面是由providerName的配置的。总算有了一点点的收获,也不一定最好,但是总算憋出来了。呵呵
- 在 appSettings 里配置字符串,不在 connectionStrings 里写连接数据库的配置
- DotNetCore跨平台~在appsettings.json里自定义配置项
- Struts数据源配置过程(不在Action里连接)
- 在easyui里写一个连接数据库的二级联动
- 在cognos的配置里,无法测试连接数据库,而且启动报错误
- 在Hibernate里配置Proxool连接池
- DotNetCore跨平台~关于appsettings.json里各种配置项的读取
- asp.net连接sql2005,cs文件里直接书写的代码,纯手工操作,不在web.config里面进行配置。
- web.config中AppSettings和ConnectionStrings的配置与区别及获取
- web.config中配置数据库连接的两种方式(<appSettings >与 <connectionStrings>)
- Asp.NET Core 在Controller调用appsettings里的值
- 在web.config里配制连接Access数据库字符串
- 在eclipse里连接数据库
- struts-console 在eclips里的配置
- struts-console 在eclips里的配置
- OPENGL在vs2010里的配置问题
- Ivy 在eclipse里的配置
- ubuntu在虚拟机里的上网配置
- 在IE下面指定表单编码方式
- const的使用
- 学习机票:国航怎么就有7折航班
- 基于 JSF+Spring + JPA 构建敏捷的Web应用[88250原创]
- 冬季恋歌
- 在 appSettings 里配置字符串,不在 connectionStrings 里写连接数据库的配置
- Eclipse基础--使用links方式安装Eclipse插件
- 今天刚刚开通个人空间
- 项目开发
- sql 多记录并单记录
- 安装eclipse也不是一帆风顺啊
- Nokia DAU-9P数据线电路图
- 通过PPG“神话” 论服装电子商务
- ASP木马、Webshell之安全防范解决办法(转贴)