C#使用SqlServer
//使用轻量级的SqlDataReader显示数据//指定Sql Server提供者的连接字符串string connString = "server=localhost;database =PGMM;uid =sa;pwd=yunfengAa";//建立连接对象SqlConnection Sqlconn = new SqlConnection(connString);//打开连接Sqlconn.Open();为上面的连接指定Command对象//SqlCommand thiscommand = Sqlconn.CreateCommand();//thiscommand.CommandText = "select customerID,companyName from customers";为指定的command对象执行DataReader//SqlDataReader thisSqlDataReader = thiscommand.ExecuteReader();只要有数据//while (thisSqlDataReader.Read())//{输出数据// Console.WriteLine("\t{0}\t{1}", thisSqlDataReader["customerId"], thisSqlDataReader["companyName"]);//}关闭读取//thisSqlDataReader.Close();关闭连接//Sqlconn.Close();//Console.ReadLine();//使用dataset显示数据// 查询字符串string thisCommand = "select * from t1";//创建SqlDataAdapter对象,有两个参数,一个是查询字符串,一个是连接对象SqlDataAdapter SqlDap = new SqlDataAdapter(thisCommand, Sqlconn);//创建DataSet对象DataSet thisDataset = new DataSet();//使用SqlDataAdapter的Fill方法填充DataSet,有两个参数,一个是创建的DataSet实例,一个是填入的表SqlDap.Fill(thisDataset, "customers");//显示查询结果foreach (DataRow theRow in thisDataset.Tables["customers"].Rows){Response.Write(theRow["c1"] + "\t");}Sqlconn.Close();Console.ReadLine(); 转载于:https://www.cnblogs.com/zzzili/p/6685298.html
本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!
