當前位置:首頁 » 編程語言 » aspnet調用sql
擴展閱讀
webinf下怎麼引入js 2023-08-31 21:54:13
堡壘機怎麼打開web 2023-08-31 21:54:11

aspnet調用sql

發布時間: 2022-05-31 14:53:22

❶ 在ASP.NET中,如何讓使用C#語言來連接sql SERVER資料庫並對資料庫進行基本的增刪查改操作

landy543210 的回答是對的,不過有些問題:
1、很顯然LZ在web應用程序上是新人,直接給代碼LZ可能不大明白;
2、LZ的是web應用程序,所以MessageBox就不要用了,要的話自己封裝一個;
3、使用sql資料庫的連接與命令,需添加引用
using System.Data.SqlClient;
4、LZ添加自己的頁面控制項與處理代碼後,然後將
landy543210的代碼在修改下以符合自己的業務需求,然後去掉消息框部分,再添加到自己的響應事件中,比如button_click

❷ 在asp.net(c#)中如何用sql語句調用伺服器端的存儲過程

如果已知了
伺服器端
有你需要的
存儲過程
,則第一步需要使你的程序和伺服器先建立起連接,連接成功之後可以寫一個
sql語句
:exec
存儲過程名(不帶參的存儲過程)就行了,當然這些可以在你的程序里寫也可以在你的程序里寫語句用來調用完成上述sql語句的存儲過程名即把上面的exec
存儲過程名
封裝在另一個存儲過程里,在程序里通過.net
里的
sqlcommand

sqlconnection
類來實現完成上面所述的語句

❸ ASPNET怎樣調用一個存儲過程(帶參數的)

ASPNET怎樣調用一個存儲過程(帶參數的)使用的是Ado.NET中的相關的資料庫操作方法。
Ado.NET連接資料庫有以下幾個步驟:
1:使用配置的資料庫連接串,創建資料庫連接 Connection 對象
2:構建操作的sql語句
3:定義command對象
4:打開數據連接
5:執行命令
舉一個例子,刪除操作
public int DeleteStudent3(int stuID)
{

int result = 0;

using (SqlConnection connection = new SqlConnection(connString))
{
SqlCommand objCommand = new SqlCommand(dboOwner + ".usp_DeleteStudent", connection);
objCommand.CommandType = CommandType.StoredProcere;

objCommand.Parameters.Add("@stuID", SqlDbType.Int).Value = stuID;

connection.Open();
result = objCommand.ExecuteNonQuery();
}
return result;
}

❹ asp C#Web,向sql插入數據,不使用存儲過程的話有必要用Parameters屬性嗎,

強烈推薦使用Parameter,不要自己拼接sql,一來提高效率,二來避免注入攻擊
比如
string sql = "select * from tb where name='"+ abc + "'";//abc 可以改成 ' or 1=1就可以攻擊了

參考存儲過程寫:
string sql = "select * from tb wherename=@name";
command.Parameters.Add(

你帖出來的代碼,就是最優寫法了,要知道,任何兩條sql,即使僅僅大小寫不同,或僅多一個空格,都被認為是不同的sql,不會重用執行計劃緩存,所以效率低,用參數化,就保證是同一個sql,效率提升了

而且拼接sql還會導致上面說的注入攻擊

❺ asp.net c#調用類訪問資料庫

數據層還需要函數,否則你這個結構就不叫三層結構了,你這個函數應該是在數據訪問層里到,數據訪問層返回給邏輯層的數據不應該是SqlDataReader對象,你可以選擇返回String[],List<....>,HashTable等。
數據訪問層:
給你幾個我的函數吧:
public
static
void
Open()
{
if
(con.State
==
ConnectionState.Closed)
{
con.Open();
}
}
public
static
void
Close()
{
if
(con.State
==
ConnectionState.Open)
{
con.Close();
}
}
public
static
SqlCommand
CreateCommand(string
sql)
{
return
new
SqlCommand(sql,
con);
}
public
static
List<string>
RunSQLReturnList(string
sql)
{
List<string>
info
=
new
List<string>();
SqlCommand
cmd
=
CreateCommand(sql);
Open();
SqlDataReader
sdr
=
cmd.ExecuteReader();
while
(sdr.Read())
{
for
(int
i
=
0;
i
<
sdr.FieldCount;
i++)
{
info.Add(sdr.GetName(i).ToString());
info.Add(sdr.GetValue(i).ToString());
}
}
Close();
return
info;
}
public
static
Hashtable
RunSQLReturnHashTable(string
sql)
{
List<string>
lt
=
RunSQLReturnList(sql);
Hashtable
ht
=
new
Hashtable();
for
(int
i
=
0;
i
<
lt.Count;
i
+=
2)
{
ht[lt[i]]
=
lt[i
+
1];
}
return
ht;
}
數據訪問層返回給邏輯層HashTable對象hashTable1:
user.username=hashTable1["Name"].ToString();
.........
邏輯層返回給表示層實體對象user
label1.text
=
user.UserName;

❻ ora-00911(PLSQL運行沒問題),但是在VS(ASP.NET[C#])中使用sqldatasource報錯。

請檢查一下是不是日期轉換格式不兼容,直接讓系統進行日期轉換。
select
min(datetime_)
as
開始日期,max(datetime_)
as
結束日期,t.bsc,sum(num5)
as
達標次數,sum(num)
as
總次數,sum(num5)/sum(num)
as
達標比例
from
SZDO.TBL_SINR_FREQ
t
where
datetime_
between
'2013-03-03'
and
'2013-03-03'
group
by
t.bsc
order
by
達標比例
desc

❼ ASP.NET中SqlClient的應用(C#)

以下是我自己的代碼,截幾段,自己看
public class DataBase
{
private string strConn;
private SqlConnection con;

//取得資料庫連接
public DataBase(string type) //type 指定連到哪個資料庫 {
strConn = ConfigurationSettings.AppSettings[type];
con=new SqlConnection(strConn);
}
public int Open()
{
try
{
con.Open();
return 0;
}
catch(System.Data.SqlClient.SqlException e)
{
con.Close();
throw new Exception(e.Message);
}
finally
{
}
}
public int Close()
{
try
{
con.Close();
return 0;
}
catch(System.Data.SqlClient.SqlException e)
{
con.Close();
throw new Exception(e.Message);
}
finally
{
}
}
//根據SqlCommand得到reader--需要先Open() 讀完後再Close()
public SqlDataReader ExecuteCmd4Reader(SqlCommand cmd)
{
cmd.Connection=con;
try
{
SqlDataReader sdr=cmd.ExecuteReader();
return sdr;
}
catch(System.Data.SqlClient.SqlException e)
{
cmd.Dispose();
con.Close();
throw new Exception(e.Message);
}
finally
{
cmd.Dispose();
}

}

//
public int ExecuteCmdNonQuery(SqlCommand cmd)
{
//SqlConnection con = new SqlConnection(strConn);
cmd.Connection=con;
try
{
con.Open();

return cmd.ExecuteNonQuery();
}
catch(System.Data.SqlClient.SqlException e)
{
throw new Exception(e.Message);
}
finally
{
cmd.Dispose();
con.Close();
}
}
//執行SqlCommand得到結果集
public DataSet ExecuteCmd4DataSet(SqlCommand cmd)
{
SqlConnection con = new SqlConnection(strConn);
cmd.Connection=con;
try
{
con.Open();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
sda.Fill(ds);
return ds;
}
catch(System.Data.SqlClient.SqlException e)
{
throw new Exception(e.Message);

}
finally
{
cmd.Dispose();
con.Close();
}
}
//執行SqlCommand得到唯一值
public object ExecuteCmdScalar(SqlCommand cmd)
{
SqlConnection con = new SqlConnection(strConn);
cmd.Connection=con;
try
{
con.Open();

return cmd.ExecuteScalar();;
}
catch(System.Data.SqlClient.SqlException e)
{
throw new Exception(e.Message);

}
finally
{
cmd.Dispose();
con.Close();
}
}

//執行幾個語句組成的事務
public int ExecuteCmds(SqlCommand[] cmds)
{
SqlConnection con = new SqlConnection(strConn);

try
{
con.Open();
}
catch(System.Data.SqlClient.SqlException e)
{
throw new Exception(e.Message);
}
SqlTransaction trans = con.BeginTransaction();

try
{
foreach(SqlCommand cmd in cmds)
{
cmd.Connection = con;
cmd.Transaction = trans;

cmd.ExecuteNonQuery();
}
trans.Commit();
return 0;
}
catch(System.Data.SqlClient.SqlException e)
{
trans.Rollback();
throw new Exception(e.Message);
}
finally
{
con.Close();
}
}

//執行資料庫操縱語句
public int ExecuteNonQuery(string strSQL)
{
SqlConnection con = new SqlConnection(strConn);
SqlCommand cmd = new SqlCommand(strSQL,con);
try
{
con.Open();
cmd.ExecuteNonQuery();
return 0;
}
catch(System.Data.SqlClient.SqlException e)
{
throw new Exception(e.Message);
}
finally
{
cmd.Dispose();
con.Close();
}
}

//執行幾個語句組成的事務
public int ExecuteSqls(string[] strSQLs)
{
SqlConnection con = new SqlConnection(strConn);
SqlCommand cmd = new SqlCommand();
//int j=strSQLs.Length;

try
{
con.Open();
}
catch(System.Data.SqlClient.SqlException e)
{
throw new Exception(e.Message);
}
SqlTransaction trans = con.BeginTransaction();

try
{
cmd.Connection = con;
cmd.Transaction = trans;

foreach(string str in strSQLs)
{
cmd.CommandText = str;
cmd.ExecuteNonQuery();
}
trans.Commit();
return 0;
}
catch(System.Data.SqlClient.SqlException e)
{
trans.Rollback();
throw new Exception(e.Message);
}
finally
{
cmd.Dispose();
con.Close();
}
}

//執行查詢語句得到結果集
public DataSet Execute4DataSet(string strSQL)
{
SqlConnection con = new SqlConnection(strConn);
try
{
con.Open();
SqlDataAdapter sda = new SqlDataAdapter(strSQL,con);
DataSet ds = new DataSet();
sda.Fill(ds);
return ds;
}
catch(System.Data.SqlClient.SqlException e)
{
throw new Exception(e.Message);

}
finally
{
con.Close();
}
}

}

///////////////////////////////////////////////////
使用:
DataBase db=new DataBase("....連接串");
string sql="select last_login,last_login_fail from admin where uid=@uid";
SqlCommand cmd=new SqlCommand(sql);
cmd.Parameters.Add("@uid",SqlDbType.Int).Value=Session["xxx"];
db.Open();
SqlDataReader sdr=db.ExecuteCmd4Reader(cmd);
if(sdr.Read())
{
this.lbl_lastlogin.Text=sdr.GetSqlDateTime(0).ToString();
this.lbl_lastfail.Text=sdr.GetSqlDateTime(1).ToString();
}
db.Close();

///////////////////////////////////////////
DataBase db=new DataBase("....連接串");
string sql;

int t;
t=page_size*(cur_page-1);
sql="查詢語句"
+" and order_id< all (select top "+t.ToString()+" order_id from orders where uid=@uid order by order_id desc) order by order_id desc";

SqlCommand cmd=new SqlCommand(sql);
cmd.Parameters.Add("@uid",SqlDbType.Int).Value=Session["xxx"];
DataSet ds=db.ExecuteCmd4DataSet(cmd);
this.dgOrders.DataSource=ds;
this.dgOrders.DataBind();

/////////////////////
DataBase db=new DataBase("ConHouse");
string sql="update cor_user set page=@page where uid=@uid";
SqlCommand cmd=new SqlCommand(sql);
cmd.Parameters.Add("@page",SqlDbType.VarChar,32).Value=this.ddlTemplate.SelectedItem.Value;
cmd.Parameters.Add("@uid",SqlDbType.Int).Value=Session["xxx"];
db.ExecuteCmdNonQuery(cmd);

///////////////////
DataBase db=new DataBase("ConHouse");
SqlCommand[] cmds=new SqlCommand[2];
cmds[0]=new SqlCommand("delete from cor_color_template where uid=@uid");
cmds[0].Parameters.Add("@uid",SqlDbType.Int).Value=Session["xxx"];
cmds[1]=new SqlCommand("insert into cor_color_template values(@uid,@color_template)");
cmds[1].Parameters.Add("@uid",SqlDbType.Int).Value=Session["xxx"];
cmds[1].Parameters.Add("@color_template",SqlDbType.VarChar,32).Value=this.ddlTemplate.SelectedValue;
db.ExecuteCmds(cmds);
Response.Write("<script language=JavaScript>alert('顏色模板設置成功!');</script>");

❽ ASP.NET(C#)高手求救!

在Visual Studio 2005連接SQL Server 2000中,我們可能會知道,連接字元串會比較簡單,一般遠程連接的時候,只需要在Web.config文件配置如下:

<appSettings>
<add key="ConnectionString" value="server=(local);database=MyDb;uid=sa;pwd=sa;Max pool size=1000;Connect
Timeout=20"/>
</appSettings>

然後在代碼中通過調用下面的語句就可以實現資料庫的連接了。

SqlConnection conn = new SqlConnection(ConfigurationSettings.AppSettings["ConnectionString"]);

在此前提只需要把身份驗證設置成為SQL Server和Windows驗證就可以了。

而在SQL Server 2005中,如果直接這樣操作就會報下面的錯誤:

在建立與伺服器的連接時出錯。在連接到 SQL Server 2005 時,在默認的設置下 SQL Server 不允許進行遠程連接可能會導致此失敗。 (provider: 命名管道提供程序, error: 40 - 無法打開到 SQL Server 的連接) 。

我們就需要單獨對SQL Server 2005進行配置,步驟如下:

1.配置SQL Server外圍應用伺服器,開啟SQL2005遠程連接功能:

操作方式如下,點擊「配置工具」->「SQL Server外圍應用配置器」,然後在打開的窗口中選擇「服務和連接的外圍應用配置器」->然後選擇Database Engine節點下的 「遠程連接」,選擇「本地連接和遠程連接」,同時選擇「同時使用TCP/IP和named pipes」,確定後然後需要重新啟動資料庫服務就可以了。

2.把登陸設置改為SQL Server 和 Windows 身份驗證模式,具體設置如下:

打開SQL Server Management Studio管理器,點擊伺服器上面右鍵然後查看屬性,在安全性選項裡面對服務身份驗證選擇「SQL Server 和 Windows 身份驗證模式」。

3.修改SQL Server sa的密碼,體設置如下:

在SQL Server Management Studio管理器中,展開伺服器上的「安全性」->登陸名->在sa帳號上點右鍵屬性,這樣在「常規」的選擇頁中更改sa登陸帳號的密碼。 注意SQL Server2005中,不允許密碼設置簡單,否則會通不過。然後在選擇頁的「狀態」的登錄修改為啟用。

4.資料庫連接字元串:

資料庫連接字元串有很多種,如:

Data Server=.\SQLEXPRESS;Initial Catalog=Northwind;User ID=sa;Password=sapassSql

Data Server=伺服器名\SQLEXPRESS;Initial Catalog=Northwind;User ID=sa;Password= sapassSql

Data Server=localhost\SQLEXPRESS;Initial Catalog=Northwind;User ID=sa;Password= sapassSql

Data Server=.;Initial Catalog=Northwind;User ID=sa;Password= sapassSql

Data Server=伺服器名;Initial Catalog=Northwind;User ID=sa;Password= sapassSql

具體的選擇是和SQL Server2005的版本有關系,如果是SQL Server 2005 Express版本,則必須要有「\SQLEXPRESS」,因此如果字元串是定義為一個變數的時候應該寫成Server=.\\SQLEXPRESS。

5.在.net2.0 Framework中注冊SQL Server資料庫:

找到.net2.0 Framework的安裝路徑,一般安裝在 「C:\Windows\Microsoft.NET\Framework\v2.0.50727」目錄下,然後在DOS中在指定目錄下運行 「ASPNET_REGSQL」指令,就會出現ASP.NET SQL Server安裝向導,點擊「下一步」,然後選擇「為應用程序服務配置SQL Server」,然後直接點擊下一步,就會完成SQL Server注冊界面。我們就會看到NorthWind資料庫下面多了幾張表。

6.設置web.config文件:

在應用程序中的web.config文件添加如下資料庫連接的配置:

<connectionStrings>
<add name="ConnectionSqlServer" connectionString="Data Source= .\SQLEXPRESS;Initial Catalog=Northwind; User ID=sa;Password= sapassSql" providerName="System.Data.SqlClient"/>
</connectionStrings>

這樣我們便在Visual Studio 2005中可以輕松的調用SQL Server的數據連接了。

現在我們來對上面所做的步驟來測試數據的連接,我們打開Visual Studio 2005,在視圖的「伺服器資源管理器」來添加SQL Server 2005的連接,通過輸入伺服器名稱和對應的SQL Server身份驗證的sa用戶名密碼後,選取對應的資料庫,然後點擊「測試連接」,就會成功的連接到資料庫了。

❾ asp.net (C#) 平台下如何加密用戶登錄密碼使用的是sql資料庫,最好是能給出詳細的步驟

有一個md5加密的方法

md5(獲取到輸入的密碼,16)

16也可以是32 這是加密後的密碼長度 然後把這個存進資料庫,,,以後登陸
同樣也是這樣 加密 和已經存在資料庫里的進行匹配,,如果想=就說明密碼真確