1 use Northwind 2 go 3 if exists(select * from sysobjects where name=‘w_cs‘) 4 drop procedure w_cs 5 go 6 create proc w_cs 7 ( 8 @id int 9 ) 10 as 11 SELECT * FROM [Northwind].[dbo].[Categories] 12 where CategoryID<@id
using System; using System.Collections.Generic; using System.Data; using System.Data.SqlClient; using System.Linq; using System.Text; namespace 使用SqlParameter给sql语句传参数 { public class myclass1 { public myclass1() { } public myclass1(int idtemp, string nametemp) { id = idtemp; name = nametemp; } public int id { get; set; } public string name { get; set; } } //定义一个类,来格式化数据库传过来的啥东东 class Program { static void Main(string[] args) { string ip = "127.0.0.1"; string db = "Northwind"; string uid = "sa"; string pwd = "sa"; string constr = "Data Source=" + ip + ";" + "Initial Catalog=" + db + ";" + "uid=" + uid + ";" + "pwd=" + pwd; SqlConnection cnn = new SqlConnection(constr); //使用SqlConnectionStringBuilder写法会简单些 string tsql = "SELECT * FROM [Northwind].[dbo].[Categories] where CategoryID<@iid";//用的语句 System.Data.SqlClient.SqlCommand tsql2 = new SqlCommand(tsql, cnn); //SqlCommand可以使用查询语句 也可以使用存储过程 tsql2.CommandType = CommandType.Text; //指定sql要执行的是语句,不是存储过程和事务;默认就是 CommandType.Text int id = 5; SqlParameter[] param = new SqlParameter[] { new SqlParameter("@iid",id), //给这个数组赋值,还可以相同格式继续添加 }; tsql2.Parameters.AddRange(param); cnn.Open(); //都定义完了开始执行了,这里要做异常捕获:免得超时报错 SqlDataReader jg = tsql2.ExecuteReader(); List<myclass1> lis = new List<myclass1>();//定义一个泛型 while (jg.Read()) { myclass1 myclass_temp = new myclass1(jg.GetInt32(0), jg.GetString(1)); lis.Add(myclass_temp); }//将数据库查询出的值逐条放入泛型中 cnn.Close();//执行完了 // tsql2.ExecuteNonQuery();//可以用这个查询出影响了几行数据,也可以用他来触发执行 foreach (myclass1 item in lis) { Console.WriteLine(item.id + item.name);//遍历出查询结果 } Console.ReadLine(); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; using System.Data.SqlClient; namespace 使用SqlParameter给存储过程传参数并显示执行结果 { public class myclass1 { public myclass1() { } public myclass1(int idtemp, string nametemp) { id = idtemp; name = nametemp; } public int id { get; set; } public string name { get; set; } } //定义一个类,来格式化数据库传过来的啥东东 class Program { static void Main(string[] args) { string ip = "127.0.0.1"; string db = "Northwind"; string uid = "sa"; string pwd = "sa"; string constr = "Data Source=" + ip + ";" + "Initial Catalog=" + db + ";" + "uid=" + uid + ";" + "pwd=" + pwd; SqlConnection cnn = new SqlConnection(constr); //使用SqlConnectionStringBuilder写法会简单些 string tsql = "w_cs";//指定存储过程名 System.Data.SqlClient.SqlCommand tsql2 = new SqlCommand(tsql, cnn); //SqlCommand可以使用查询语句 也可以使用存储过程 tsql2.CommandType = CommandType.StoredProcedure; //指定sql要执行的是存储过程 int id = 5; SqlParameter[] param = new SqlParameter[] { new SqlParameter("@id",id), //给这个数组赋值,还可以相同格式继续添加 @id 是存储过程中的变量 //new SqlParameter("@office_am",office_am), //new SqlParameter("@office_pm",office_pm), //new SqlParameter("@factory_am",factory_am), //new SqlParameter("@factory_pm",factory_pm), //new SqlParameter("@memo",memo) }; tsql2.Parameters.AddRange(param); cnn.Open(); //都定义完了开始执行了,这里要做异常捕获:免得超时报错 SqlDataReader jg = tsql2.ExecuteReader(); List<myclass1> lis = new List<myclass1>();//定义一个泛型 while (jg.Read()) { myclass1 myclass_temp = new myclass1(jg.GetInt32(0), jg.GetString(1)); lis.Add(myclass_temp); }//将数据库查询出的值逐条放入泛型中 cnn.Close();//执行完了 // tsql2.ExecuteNonQuery();//可以用这个查询出影响了几行数据,也可以用他来触发执行 foreach (myclass1 item in lis) { Console.WriteLine(item.id + item.name);//遍历出查询结果 } Console.ReadLine(); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Data; using System.Data.SqlClient; namespace 使用DataSet将数据读取到本地缓存2 { class Program { static void Main(string[] args) { string ip = "127.0.0.1"; string db = "Northwind"; string uid = "sa"; string pwd = "sa"; string constr = "Data Source=" + ip + ";" + "Initial Catalog=" + db + ";" + "uid=" + uid + ";" + "pwd=" + pwd; SqlConnection cnn = new SqlConnection(constr); //使用SqlConnectionStringBuilder写法会简单些 string tsql = "w_cs";//指定存储过程名 //SqlCommand tsql2 = new SqlCommand(); //tsql2.Connection = cnn; //tsql2.CommandText = tsql; //SqlCommand tsql2 = new SqlCommand(tsql, cnn); //这个可以看做简写 SqlDataAdapter da = new SqlDataAdapter(tsql,cnn); // da.SelectCommand = tsql2; da.SelectCommand.CommandType = CommandType.StoredProcedure; int id = 5; SqlParameter[] param = new SqlParameter[] { new SqlParameter("@id",id) }; da.SelectCommand.Parameters.AddRange(param); //tsql2.Parameters.AddRange(param); DataSet ds1 = new DataSet(); da.Fill(ds1); foreach (DataRow item in ds1.Tables[0].Rows) { Console.WriteLine(item[0].ToString()); } Console.ReadLine(); } } }
数据读取下来了,还可以写个类,传给集合,从集合里得到值
原文:https://www.cnblogs.com/myjobok/p/10355739.html