asp.net連接查詢SQL數(shù)據(jù)庫并把結(jié)果顯示在網(wǎng)頁上(2種方法)
更新時(shí)間:2013年04月26日 15:51:49 作者:
使用C#連接SQL數(shù)據(jù)庫,并使用SQL語句查詢,摸索了兩天終于運(yùn)行起來了,接下來為大家分享下兩種連接方法,感興趣的朋友可以參考下哈,希望可以幫助到你
在ASP.NET中,使用C#連接SQL數(shù)據(jù)庫,并使用SQL語句查詢,以前從來沒有接觸過C#,最近用到了,摸索了兩天終于運(yùn)行起來了,Mark一下,不喜勿噴
有兩種方法:(說的是第一種方法不安全,我也不清楚^_^)
第一種方法:
//建立ASP.NET Web 應(yīng)用程序,直接在Page_load函數(shù)中加入一下代碼,貌似就可以用了
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='" + username + "'";
SqlDataAdapter adapter = new SqlDataAdapter(strSQL, con);
DataSet ds = new DataSet();
adapter.Fill(ds);
foreach (DataRowView drv in ds.Tables[0].DefaultView)
{
Response.Write(drv["第一個(gè)字段"]+"|"+drv["第二個(gè)字段"]);
}
}
}
第二種方法說的比較安全,就是比較麻煩
//1、修改Web.config配置文件
<configuration>
<connectionStrings>
</connectionStrings>
//下面三行是添加的內(nèi)容,即連接數(shù)據(jù)庫的信息
<appSettings>
<add key="connect" value="server=.;database=NorthWind;uid=sa;pwd=sa;"/>
</appSettings>
<system.web>
//2、連接數(shù)據(jù)庫
sCon = ConfigurationManager.AppSettings["connect"];
if (string.IsNullOrEmpty(sCon))
{
Response.Write("連接字符串為空!");
}
con = new SqlConnection(sCon);
//3、打開數(shù)據(jù)庫
if (con.State == ConnectionState.Closed)
con.Open();
//4、查詢函數(shù)
public SqlDataReader ExcuteDataReader(string strTxt, CommandType cmdType, SqlParameter[] Params)
{
SqlDataReader dr = null;
if (con.State == ConnectionState.Closed)
{
Response.Write("數(shù)據(jù)庫的連接沒有打開!");
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(param);
}
}
#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、執(zhí)行查詢
//SQL語句,id=N'id',加個(gè)N是為了能識(shí)別中文字符。
string s = "select * from table where id=N'" + id + "'";
SqlParameter[] Params1 = null;
//保存結(jié)果
SqlDataReader select_result = null;
select_result = a.ExcuteDataReader(s, CommandType.Text, Params1);
string ss = "";
while (select_result.Read())
{
//根據(jù)自己的字段數(shù)寫
ss = ss + "第一個(gè)字段:" + select_result[0] + ", 第二個(gè)字段:" + select_result[1] + "; ";
}
//測試輸出
Response.Write(ss);
有兩種方法:(說的是第一種方法不安全,我也不清楚^_^)
第一種方法:
復(fù)制代碼 代碼如下:
//建立ASP.NET Web 應(yīng)用程序,直接在Page_load函數(shù)中加入一下代碼,貌似就可以用了
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='" + username + "'";
SqlDataAdapter adapter = new SqlDataAdapter(strSQL, con);
DataSet ds = new DataSet();
adapter.Fill(ds);
foreach (DataRowView drv in ds.Tables[0].DefaultView)
{
Response.Write(drv["第一個(gè)字段"]+"|"+drv["第二個(gè)字段"]);
}
}
}
第二種方法說的比較安全,就是比較麻煩
復(fù)制代碼 代碼如下:
//1、修改Web.config配置文件
<configuration>
<connectionStrings>
</connectionStrings>
//下面三行是添加的內(nèi)容,即連接數(shù)據(jù)庫的信息
<appSettings>
<add key="connect" value="server=.;database=NorthWind;uid=sa;pwd=sa;"/>
</appSettings>
<system.web>
//2、連接數(shù)據(jù)庫
sCon = ConfigurationManager.AppSettings["connect"];
if (string.IsNullOrEmpty(sCon))
{
Response.Write("連接字符串為空!");
}
con = new SqlConnection(sCon);
//3、打開數(shù)據(jù)庫
if (con.State == ConnectionState.Closed)
con.Open();
//4、查詢函數(shù)
public SqlDataReader ExcuteDataReader(string strTxt, CommandType cmdType, SqlParameter[] Params)
{
SqlDataReader dr = null;
if (con.State == ConnectionState.Closed)
{
Response.Write("數(shù)據(jù)庫的連接沒有打開!");
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(param);
}
}
#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、執(zhí)行查詢
//SQL語句,id=N'id',加個(gè)N是為了能識(shí)別中文字符。
string s = "select * from table where id=N'" + id + "'";
SqlParameter[] Params1 = null;
//保存結(jié)果
SqlDataReader select_result = null;
select_result = a.ExcuteDataReader(s, CommandType.Text, Params1);
string ss = "";
while (select_result.Read())
{
//根據(jù)自己的字段數(shù)寫
ss = ss + "第一個(gè)字段:" + select_result[0] + ", 第二個(gè)字段:" + select_result[1] + "; ";
}
//測試輸出
Response.Write(ss);
您可能感興趣的文章:
- ASP.NET web.config中數(shù)據(jù)庫連接字符串connectionStrings節(jié)的配置方法
- ASP.NET 6種常用數(shù)據(jù)庫的連接方法
- ASP.NET2.0 SQL Server數(shù)據(jù)庫連接詳解
- ASP.NET連接數(shù)據(jù)庫并獲取數(shù)據(jù)方法總結(jié)
- ASP.NET連接MySql數(shù)據(jù)庫的2個(gè)方法及示例
- ASP.NET中操作SQL數(shù)據(jù)庫(連接字符串的配置及獲取)
- asp.net連接數(shù)據(jù)庫 增加,修改,刪除,查詢代碼
- ASP.NET 連接ACCESS數(shù)據(jù)庫的簡單方法
- ASP.NET連接 Access數(shù)據(jù)庫的幾種方法
- ASP.NET WebAPI連接數(shù)據(jù)庫的方法
相關(guān)文章
附加到SQL2012的數(shù)據(jù)庫就不能再附加到低于SQL2012的數(shù)據(jù)庫版本的解決方法
這篇文章主要介紹了附加到SQL2012的數(shù)據(jù)庫就不能再附加到低于SQL2012的數(shù)據(jù)庫版本的解決方法,需要的朋友可以參考下2014-02-02
sql 查詢結(jié)果合并union all用法_數(shù)據(jù)庫技巧
sql語句查詢結(jié)果合并union all用法_數(shù)據(jù)庫技巧,需要的朋友可以參考下。2009-11-11
SQL Server存儲(chǔ)過程生成insert語句實(shí)例
這篇文章主要介紹了SQL Server使用存儲(chǔ)過程生成insert語句再執(zhí)行大量插入數(shù)據(jù)的方法2013-11-11
sql拆分字符串實(shí)現(xiàn)一行變多行的實(shí)例代碼
本文主要介紹了sql拆分字符串實(shí)現(xiàn)一行變多行的實(shí)例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2024-09-09
SQL Server實(shí)現(xiàn)split函數(shù)分割字符串功能及用法示例
這篇文章主要介紹了SQL Server實(shí)現(xiàn)split函數(shù)分割字符串功能及用法,結(jié)合實(shí)例形式分析了SQL Server實(shí)現(xiàn)split分割字符串的相關(guān)技巧與使用方法,需要的朋友可以參考下2016-08-08
Sql Server的一些知識(shí)點(diǎn)定義總結(jié)
這篇文章主要給大家總結(jié)介紹了關(guān)于Sql Server的一些知識(shí)點(diǎn)定義文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-12-12

