人人做人人澡人人爽欧美,国产主播一区二区,久久久精品五月天,羞羞视频在线观看免费

當前位置:蘿卜系統(tǒng)下載站 > 技術(shù)開發(fā)教程 > 詳細頁面

在asp.net中設(shè)置sql-server數(shù)據(jù)庫的一些小技巧

在asp.net中設(shè)置sql-server數(shù)據(jù)庫的一些小技巧

更新時間:2022-08-08 文章作者:未知 信息來源:網(wǎng)絡(luò) 閱讀次數(shù):

 

1.給數(shù)據(jù)庫語句參數(shù)傳遞

向數(shù)據(jù)庫操作語句傳遞參數(shù)可以通過存儲過程實現(xiàn),這里給出另外兩種簡便易捷的方法:

可以在C#中通過字符串操作將參數(shù)直接傳入SQL語句變量中,例如:

string s="Davolio";

string sql= "select * from employees where LastName="+"'"+s+"'"

相當于寫入SQL語句:

select * from employees where LastName='Davolio'
    也可以通過thisCommand.Parameters.Add()方法實現(xiàn),如下所示:

string s="Davolio";
            
SqlConnection thisConnection=new SqlConnection

("Data Source=(local);Initial Catalog=Northwind;UID=sa;PWD=");

thisConnection.Open ();

SqlCommand thisCommand=thisConnection.CreateCommand ();
              
thisCommand.CommandText =

" select * from employees where LastName=@charname";

thisCommand.Parameters.Add("@charname",s);

 

 

可以看到,字符串s將參數(shù)“Ddbolio”傳遞給數(shù)據(jù)庫操作語句中的參數(shù)charname。

2.將數(shù)據(jù)庫中不同表內(nèi)的數(shù)據(jù)讀入到數(shù)據(jù)集DataSet中

SqlDataAdapterFill方法可以填充已知數(shù)據(jù)集,并且為每個填充項創(chuàng)建一個臨時表,可以通過對該表的訪問來讀取數(shù)據(jù)集中的相關(guān)數(shù)據(jù)。其相關(guān)操作如下所示:

SqlConnection thisConnection=new SqlConnection

("Data Source=(local);Initial Catalog=Northwind;UID=sa;PWD=");

try

{

thisConnection.Open ();

}

catch(Exception ex)

{

thisConnection.Close ();

}

 

string sql1="select * from employees";

string sql2="select * from Customers";

SqlDataAdapter sda=new SqlDataAdapter(sql1,thisConnection);

DataSet ds= new DataSet();

sda.Fill(ds,"myemployees");

sda.Dispose();
          
SqlDataAdapter sda1=new SqlDataAdapter(sql2,thisConnection);

sda1.Fill(ds,"myCustomers");

sda1.Dispose();

string t1=ds.Tables["myemployees"].Rows[0]["Hiredate"].ToString();

string t2=ds.Tables["myCustomers"].Rows[0]["ContactTitle"].ToString();

Page.RegisterStartupScript("aa","<script language=javascript>alert('t1="+t1+",t2="+t2+"');</script>");

可以看到,在數(shù)據(jù)集ds中新生成了兩個臨時表“myemployees”和“myCustomers”。為驗證這兩個表中數(shù)據(jù)確實已讀入數(shù)據(jù)集ds中,通過數(shù)據(jù)讀取操作將表“myemployees”中對應于屬性“Hiredate”的第一行賦值給字符型變量t1,將表“myCustomers”中對應于屬性“ContactTitle”的第一行賦值給字符型變量t2,并通過JavaStript函數(shù)“alert()”將這些變量顯示到彈出窗口中。Page.RegisterStartupScript方法用于發(fā)出客戶端腳本塊,其第一個參數(shù)為標志位,用戶可以任意選取,第二個參數(shù)為JavaScript腳本,這里alert函數(shù)用來彈出MessageBox對話框,我們將參數(shù)t1和t2傳入該腳本中,使其在MessageBox中顯示出來。


溫馨提示:喜歡本站的話,請收藏一下本站!

本類教程下載

系統(tǒng)下載排行

網(wǎng)站地圖xml | 網(wǎng)站地圖html
主站蜘蛛池模板: 镇远县| 绥芬河市| 原阳县| 海南省| 大同县| 宁津县| 永安市| 长丰县| 句容市| 北京市| 北票市| 抚宁县| 紫云| 南雄市| 景德镇市| 太谷县| 红安县| 新绛县| 清丰县| 且末县| 宜城市| 陇西县| 突泉县| 都兰县| 通化市| 理塘县| 乌鲁木齐县| 景东| 洛南县| 乌海市| 于田县| 阿瓦提县| 凌海市| 肇州县| 柘荣县| 定陶县| 如皋市| 高邑县| 称多县| 绥滨县| 泸州市|