我用oledb方法连接数据库,总是连接不上,在网上找了半天也没找到原因,而我用sqlconnection连接则没有问题
代码如下:protected void sub(object serder, EventArgs e)
{
string s = "Provider=SQLOLEDB;server=loaclhost;database=pubs;uid=sa;pwd=123456";
OleDbConnection oledaconnecion = new OleDbConnection( s);
OleDbCommand oledacommand = new OleDbCommand();
OleDbDataReader read;
oledaconnecion.Open();
oledacommand.Connection = oledaconnecion;
oledacommand.CommandText = "select*from Employees";
read = oledacommand.ExecuteReader();
do
{
read.Read();
lab.Text += read.GetInt32(0) + "<br>";
} while (read.NextResult());
oledaconnecion.Close();
oledaconnecion = null;
}
下面是用sqlconnection连接的,连接没问题,但访问还是有点小问题protected void sqlcon(object serder, EventArgs e)
{
string s;
s = "server=localhost;database=Northwind;uid=sa;pwd=123456";
SqlConnection sqlconnection = new SqlConnection(s);
sqlconnection.Open();
SqlCommand cmd = new SqlCommand();
SqlDataReader read;
cmd.Connection = sqlconnection;
cmd.CommandText = "select*from Employees";
read=cmd.ExecuteReader();
do
{
read.Read();
lab.Text += read.GetInt32(0) + "<br>"; } while (read.NextResult());
sqlconnection.Close();
sqlconnection = null;
}就是这个代码只能读取数据库的第一行的数据,其中第一行字段信息是:1 Davolio Nancy Sales ntative 每次按提交只能显示第一行的1 ,其他行的信息就显示不出来,这又是什么原因呢?
代码如下:protected void sub(object serder, EventArgs e)
{
string s = "Provider=SQLOLEDB;server=loaclhost;database=pubs;uid=sa;pwd=123456";
OleDbConnection oledaconnecion = new OleDbConnection( s);
OleDbCommand oledacommand = new OleDbCommand();
OleDbDataReader read;
oledaconnecion.Open();
oledacommand.Connection = oledaconnecion;
oledacommand.CommandText = "select*from Employees";
read = oledacommand.ExecuteReader();
do
{
read.Read();
lab.Text += read.GetInt32(0) + "<br>";
} while (read.NextResult());
oledaconnecion.Close();
oledaconnecion = null;
}
下面是用sqlconnection连接的,连接没问题,但访问还是有点小问题protected void sqlcon(object serder, EventArgs e)
{
string s;
s = "server=localhost;database=Northwind;uid=sa;pwd=123456";
SqlConnection sqlconnection = new SqlConnection(s);
sqlconnection.Open();
SqlCommand cmd = new SqlCommand();
SqlDataReader read;
cmd.Connection = sqlconnection;
cmd.CommandText = "select*from Employees";
read=cmd.ExecuteReader();
do
{
read.Read();
lab.Text += read.GetInt32(0) + "<br>"; } while (read.NextResult());
sqlconnection.Close();
sqlconnection = null;
}就是这个代码只能读取数据库的第一行的数据,其中第一行字段信息是:1 Davolio Nancy Sales ntative 每次按提交只能显示第一行的1 ,其他行的信息就显示不出来,这又是什么原因呢?
http://www.connectionstrings.com/sql-server#p20
------------
换了
string s = "Provider=SQLOLEDB;Data Source= .;database=pubs;uid=sa;pwd=123456";
string s = "Provider=SQLOLEDB;server=loaclhost;database=pubs;uid=sa;pwd=123456";
------------
换了
string s = "Provider=SQLOLEDB;Data Source= .;database=pubs;uid=sa;pwd=123456";
这样还是不行啊,哎,初学asp.net 竟然遇到了这样的麻烦
又会出现下面情况:
+ ServerVersion “oledaconnecion.ServerVersion”引发了“System.InvalidOperationException”类型的异常 string {System.InvalidOperationException}
+ base {"无效操作。连接被关闭。"} System.SystemException {System.InvalidOperationException}