使用OLE DB数据提供程序 报错“对象名***无效”
Console.WriteLine("---使用SQL Server数据提供程序"); string sqlConnection = @"Data Source=CYY-PC\SQLEXPRESS;Integrated Security=SSPI;Initial Catalog=mydb1;";
string sqlSelect = "select * from person where age>=@Age";
SqlConnection conn=new SqlConnection(sqlConnection);
SqlCommand command = new SqlCommand(sqlSelect, conn);
command.Parameters.Add("@Age", SqlDbType.Int);
command.Parameters["@Age"].Value = 20;
SqlDataAdapter adapter = new SqlDataAdapter(command);
DataSet ds = new DataSet();
adapter.FillSchema(ds, SchemaType.Source,"Schema");
adapter.Fill(ds,"T");
foreach (DataRow row in ds.Tables["T"].Rows)
{
Console.WriteLine("ID={0},Name={1},Age={2}",row["ID"],row["name"],row["age"]);
}
Console.WriteLine("---使用OLE DB数据提供程序");
string oledbConnectionString = @"Provider=SQLOLEDB;Data Source=CYY-PC\SQLEXPRESS;Integrated Security=SSPI;Intial Catalog=mydb1;";
string oledbSelect = "Select * from person where age>=?";
OleDbConnection oledbConn = new OleDbConnection(oledbConnectionString);
OleDbCommand oledbCmd = new OleDbCommand(oledbSelect, oledbConn);
oledbCmd.Parameters.Add("@Age", OleDbType.Currency);
oledbCmd.Parameters["@Age"].Value = 20;
OleDbDataAdapter oledbDa = new OleDbDataAdapter(oledbCmd);
DataTable dt = new DataTable();
oledbDa.Fill(dt);//运行时报错,“对象名 'person' 无效。”为什么?SQL Server提供程序没错,OLE DB提供程序出错。是否有什么要注意的?请教!
foreach (DataRow row in dt.Rows)
{
Console.WriteLine("ID={0},Name={1},Age={2}", row["ID"], row["name"], row["age"]);
}