sql里的参数传递的简单例子

1 using System;
2  using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Data.OleDb;
6 using System.Data;
7
8 namespace sqlcanshu {
9 class Program {
10 public static OleDbConnection condb() {
11 return new OleDbConnection("provider=microsoft.jet.oledb.4.0;data source=db1.MDB");
12 }
13 static void Main(string[] args) {
14 OleDbConnection mycon = condb();
15 OleDbCommand mycom = new OleDbCommand();
16 mycom.Connection = mycon;
17 mycon.Open();
18 mycom.CommandText = "select * from table1 where id = @id";
19 OleDbParameter mypara = new OleDbParameter("@id",SqlDbType.Int);
20 mypara.Value = 2;
21 mycom.Parameters.Add(mypara);
22 ///com的
23 //OleDbDataReader mydr = mycom.ExecuteReader();
24 //while (mydr.Read()) {
25 // Console.WriteLine(mydr["name"].ToString());
26 //}
27 ///ds的
28 OleDbDataAdapter da = new OleDbDataAdapter(mycom);
29 DataSet ds = new DataSet();
30 da.Fill(ds);
31 Console.WriteLine(ds.Tables[0].Rows[0]["name"].ToString());
32 mycon.Close();
33 Console.Read();
34 }
35 }
36 }

 

有待扩展

posted on 2011-01-03 16:14  卑鄙De小贝  阅读(481)  评论(0)    收藏  举报

导航