数据库连接问题 [英] problem in database connectivity
本文介绍了数据库连接问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在运行此代码时,出现" executereader:连接属性尚未初始化"的运行时错误
I am getting run time error of "executereader:connection property has not been initialized", while running this code
private void Form1_Load(object sender, EventArgs e)
{
}
private void textBox1_TextChanged(object sender, EventArgs e)
{
}
private void button1_Click(object sender, EventArgs e)
{
try
{
string sconnection;
sconnection = "provider=microsoft.jet.oledb.4.0;" + "data source=intuitive.mdb";
OleDbConnection dbconn;
dbconn = new OleDbConnection(sconnection);
dbconn.Open();
OleDbCommand cmd = new OleDbCommand();
OleDbDataReader dr = null;
dr = cmd.ExecuteReader();
cmd.CommandText = "select * from table";
// dr = cmd.ExecuteReader();
string userText = textBox1.Text;
string passText = textBox2.Text;
while (dr.Read())
{
if (this.CompareStrings(dr["username"].ToString(), userText) &&
this.CompareStrings(dr["password"].ToString(), passText))
{
MessageBox.Show("OK");
}
else
{
MessageBox.Show("Error");
}
}
dr.Close();
dbconn.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
请帮我解决这个问题.
Please help me out of this problem.
推荐答案
互换以下代码:
从:
Interchange the following codes:
FROM:
dr = cmd.ExecuteReader();
cmd.CommandText = "select * from table";
收件人:
TO:
cmd.CommandText = "select * from table";
dr = cmd.ExecuteReader();
问候,
Eduard
Regards,
Eduard
您的连接字符串有问题,请尝试以下操作.
There is problem in your connection string, try following.
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\intuitive.mdb;User Id=UserID;Password=PWD;
您必须在执行任何查询之前初始化OleDbCommand对象的Connection属性.执行查询.
You have to initialize Connection Property of OleDbCommand object before executing any query.Write this code before executing your query.
cmd.Connection = dbconn
然后仍然会出现错误,请检查连接字符串.
请查看以下链接:连接字符串:)
and after that still there will be error then please check Connection String.
Check out following link :Connection Strings :)
这篇关于数据库连接问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文