1.给数据库语句参数传递 向数据库操作语句传递参数可以通过存储过程实现,这里给出另外两种简便易捷的方法: 可以在C#中通过字符串操作将参数直接传入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);
2.将数据库中不同表内的数据读入到数据集DataSet中 SqlDataAdapter的Fill方法可以填充已知数据集,并且为每个填充项创建一个临时表,可以通过对该表的访问来读取数据集中的相关数据。其相关操作如下所示:
("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 t2=ds.Tables["myCustomers"].Rows[0]["ContactTitle"].ToString();
Page.RegisterStartupScript("aa","<script language=javascript>alert('t1="+t1+",t2="+t2+"');</script>");
ps:由于网络速度太慢,不能将相关的显示图表传到服务器,真一大遗憾。还有不知道编写代码的样式和格式,使得给出的代码显得很零乱。 |
温馨提示:喜欢本站的话,请收藏一下本站!