可能是我说的不清楚 我是再vs.net 环境下做的,在窗体中拖了一个DataGrid,然后在DataGrid1_PreRender事件中写了上述代码. 错误显示:找不到类型或命名空间名称“Conn”(是否缺少 using 指令或程序集引用?) 找不到类型或命名空间名称“OleDbConnection”(是否缺少 using 指令或程序集引用?) 找不到类型或命名空间名称“SqlDataAdapter”(是否缺少 using 指令或程序集引用?)
1,OleDbConnection Conn = new OleDbConnection(@"Provider=Microsoft.Jet.OleDB.4.0;Data Source=c:\1\db1.mdb");2,瞧瞧我过去写的hello world,呵呵,包含了三个用法的示例,表名和库名你自己改下:using System; using System.Data.OleDb;public class DataReader { public static int Main(string[] args) { string dbStr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\dbLib\db1.mdb"; string cmdStr = @"select id,myUn,left(tTitle,20) as tTitle from Indexs where id>400"; // string cmdStr = @"update Indexs set myUn='ok' where id=7"; //~ExecuteNonQuery // string cmdStr = @"select count(id) from Indexs"; //~ExecuteScalar if (args.Length>0){ if (args[0] == "/src"){ Console.WriteLine(dbStr); return 0; } } OleDbConnection ObjConn = new OleDbConnection(dbStr); try{ ObjConn.Open(); OleDbCommand ObjCMD = new OleDbCommand(cmdStr,ObjConn); OleDbDataReader ObjRS = ObjCMD.ExecuteReader(); while(ObjRS.Read()){ //~用文本序列速度慢,100万条记录所需3.9秒 //Console.WriteLine("ID:{0},tTitle:{1},myUn:{2}",ObjRS["ID"],ObjRS["tTitle"],ObjRS["myUn"]); //~用数字序列速度快,100万条记录所需0.67秒 Console.WriteLine("ID:{0},tTitle:{1},myUn:{2}",ObjRS[0],ObjRS[2],ObjRS[1]); } ObjRS.Close(); // ObjCMD.ExecuteNonQuery(); // Console.WriteLine(ObjCMD.ExecuteScalar().ToString()); ObjConn.Close(); }catch(Exception e){ Console.WriteLine("\tWaring!!! oldDB Command Error:\n\n" + e); } return 0; } }
DataGrid1_PreRender事件中OleDbConnection Conn = new OleDbConnection("Provider=Microsoft.Jet.OleDB.4.0;Data Source="c:\1\db1.mdb"");
Conn.open();
strSql="select * from 某张表";
DataSet DS=new DataSet();
SqlDataAdapter adapter=new SqlDataAdapter(strSql,Conn);
adapter.Fill(DS);
DataGrid1.DataSource=DS;
DataGrid1.DataBind();
DataSet DS=new DataSet();
Adpater.Fill(DS);datagrid1.DataSource=DS;
显示Data Source="c:\1\db1.mdb"");
E:\c#\2\3\WebForm1.aspx.cs(56): 无效的表达式项“)”
也不行啊
是不是还却using System......,具体还要加什么?
我是再vs.net 环境下做的,在窗体中拖了一个DataGrid,然后在DataGrid1_PreRender事件中写了上述代码.
错误显示:找不到类型或命名空间名称“Conn”(是否缺少 using 指令或程序集引用?)
找不到类型或命名空间名称“OleDbConnection”(是否缺少 using 指令或程序集引用?)
找不到类型或命名空间名称“SqlDataAdapter”(是否缺少 using 指令或程序集引用?)
using System.Data.OleDb;public class DataReader
{
public static int Main(string[] args)
{
string dbStr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=..\dbLib\db1.mdb";
string cmdStr = @"select id,myUn,left(tTitle,20) as tTitle from Indexs where id>400";
// string cmdStr = @"update Indexs set myUn='ok' where id=7"; //~ExecuteNonQuery
// string cmdStr = @"select count(id) from Indexs"; //~ExecuteScalar if (args.Length>0){
if (args[0] == "/src"){
Console.WriteLine(dbStr);
return 0;
}
} OleDbConnection ObjConn = new OleDbConnection(dbStr); try{
ObjConn.Open();
OleDbCommand ObjCMD = new OleDbCommand(cmdStr,ObjConn); OleDbDataReader ObjRS = ObjCMD.ExecuteReader();
while(ObjRS.Read()){
//~用文本序列速度慢,100万条记录所需3.9秒
//Console.WriteLine("ID:{0},tTitle:{1},myUn:{2}",ObjRS["ID"],ObjRS["tTitle"],ObjRS["myUn"]);
//~用数字序列速度快,100万条记录所需0.67秒
Console.WriteLine("ID:{0},tTitle:{1},myUn:{2}",ObjRS[0],ObjRS[2],ObjRS[1]);
}
ObjRS.Close();
// ObjCMD.ExecuteNonQuery();
// Console.WriteLine(ObjCMD.ExecuteScalar().ToString()); ObjConn.Close();
}catch(Exception e){
Console.WriteLine("\tWaring!!! oldDB Command Error:\n\n" + e);
} return 0;
}
}