Web连接数据库方法.doc





《Web连接数据库方法.doc》由会员分享,可在线阅读,更多相关《Web连接数据库方法.doc(7页珍藏版)》请在淘文阁 - 分享文档赚钱的网站上搜索。
1、7.利用建立ASP.NET Web应用程序来连接数据库代码如下:/建立ASP.NET Web 应用程序,直接在Page_load函数中加入一下代码,貌似就可以用了 public void Page_Load(object sender, EventArgs e) using (SqlConnection con = new SqlConnection(Data Source=.;uid=sa;pwd=sa;Database=NorthWind) string username = forever; string strSQL = select * from table where name=
2、+ username + ; SqlDataAdapter adapter = new SqlDataAdapter(strSQL, con); DataSet ds = new DataSet(); adapter.Fill(ds); foreach (DataRowView drv in ds.Tables0.DefaultView) Response.Write(drv第一个字段+|+drv第二个字段); 8.通过修改 Web.config配置文件来连接数据库/1、修改Web.config配置文件 /下面三行是添加的内容,即连接数据库的信息 /2、连接数据库 sCon = Configu
3、rationManager.AppSettingsconnect; if (string.IsNullOrEmpty(sCon) Response.Write(连接字符串为空!); con = new SqlConnection(sCon); /3、打开数据库 if (con.State = ConnectionState.Closed) con.Open(); /4、查询函数 public SqlDataReader ExcuteDataReader(string strTxt, CommandType cmdType, SqlParameter Params) SqlDataReader
4、dr = null; if (con.State = ConnectionState.Closed) Response.Write(数据库的连接没有打开!); return dr; SqlCommand cmd = new SqlCommand(); cmd.Connection = con; cmd.CommandText = strTxt; cmd.CommandType = cmdType; if (Params != null) foreach (SqlParameter param in Params) if (param != null) cmd.Parameters.Add(pa
5、ram); #if NOTALLOWEXCEPTION try #endif if (cmd.ExecuteScalar() != null) dr = cmd.ExecuteReader(); #if NOTALLOWEXCEPTION catch(SqlException se) _objToShowErr = se; _sError = se.Message; return null; finally #endif cmd.Dispose(); return dr; /5、执行查询 /SQL语句,id=Nid,加个N是为了能识别中文字符。 string s = select * from
6、 table where id=N + id + ; SqlParameter Params1 = null; /保存结果 SqlDataReader select_result = null; select_result = a.ExcuteDataReader(s, CommandType.Text, Params1); string ss = ; while (select_result.Read() /根据自己的字段数写 ss = ss + 第一个字段: + select_result0 + , 第二个字段: + select_result1 + ; ; /测试输出 Response.
7、Write(ss); 9.利用VS2010连接 ACCESSVS2010 连接ACCESS,SQL SERVER,ORACLE数据库测试通过。C#连接数据库:首先新建立一个项目,填加一个类,专门用做数据库连接用。这个类起名叫DataBase.cs,下面是这个类的内容。usingSystem; usingSystem.Data; usingSystem.Configuration; usingSystem.Web; usingSystem.Web.Security; usingSystem.Web.UI; usingSystem.Web.UI.WebControls; usingSystem.W
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- Web 连接 数据库 方法

限制150内