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' string s="Davolio"; ("Data Source=(local);Initial Catalog=Northwind;UID=sa;PWD="); thisConnection.Open (); SqlCommand thisCommand=thisConnection.CreateCommand (); " 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中 SqlDataAdapter的Fill方法可以填充已知數(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(); 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中顯示出來。 |
溫馨提示:喜歡本站的話,請收藏一下本站!