oledb 连接字符串未初始化
oledb connection string not initialized
namespace PCMS
{
public partial class frmPlayerInterface : Form
{
private OleDbConnection con = new OleDbConnection();
OleDbCommand com = new OleDbCommand();
private DataTable dt = new DataTable();
public frmPlayerInterface(string getUser)
{
InitializeComponent();
con.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\Projects\SDP\PCMS\SDP.accdb";
lblUser.Text = getUser;
}
private void btnEnquire_Click(object sender, EventArgs e)
{
frmEnquire frmenq = new frmEnquire();
frmenq.ShowDialog();
}
private void btnTopUp1_Click(object sender, EventArgs e)
{
frmTopUp frmTU = new frmTopUp();
frmTU.ShowDialog();
}
private void frmPlayerInterface_Load(object sender, EventArgs e)
{
con.Open();
OleDbCommand comm = new OleDbCommand();
String sql = "select Balance from PlayerAccount where Player_User=@user";
comm.Parameters.Add(new OleDbParameter("user", lblUser.Text));
comm.CommandText = sql;
OleDbDataReader cursor = comm.ExecuteReader();
while (cursor.Read())
{
lblBalance.Text = cursor["Balance"].ToString();
}
con.Close();
}
}
}
嘿,抱歉,大家又问这个了,但我在过去三个小时里一直在尝试这个问题,并挥舞着白旗。仍然出现相同的错误。
我只想让从数据库中选择的余额值显示在标签中。
谢谢 ><
您没有将连接与命令对象相关联:
con.Open();
String sql = "select Balance from PlayerAccount where Player_User=@user";
OleDbCommand comm = new OleDbCommand(sql, con);
请注意,重用连接并不总是最好的设计。连接在 .NET 中汇集在一起,因此重新创建它们通常不是一项昂贵的操作。更好的设计是将 连接字符串 存储为 class 属性 然后在需要时创建连接:
private string ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\Projects\SDP\PCMS\SDP.accdb";
// or better yet - pull form app.config...
当你使用它时:
String sql = "select Balance from PlayerAccount where Player_User=@user";
using(OleDbConnection con = new OleDbConnection(ConnectionString))
{
con.Open();
using(OleDbCommand comm = new OleDbCommand(sql, con))
{
... Add parameters, execute query, return results
}
}
namespace PCMS
{
public partial class frmPlayerInterface : Form
{
private OleDbConnection con = new OleDbConnection();
OleDbCommand com = new OleDbCommand();
private DataTable dt = new DataTable();
public frmPlayerInterface(string getUser)
{
InitializeComponent();
con.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\Projects\SDP\PCMS\SDP.accdb";
lblUser.Text = getUser;
}
private void btnEnquire_Click(object sender, EventArgs e)
{
frmEnquire frmenq = new frmEnquire();
frmenq.ShowDialog();
}
private void btnTopUp1_Click(object sender, EventArgs e)
{
frmTopUp frmTU = new frmTopUp();
frmTU.ShowDialog();
}
private void frmPlayerInterface_Load(object sender, EventArgs e)
{
con.Open();
OleDbCommand comm = new OleDbCommand();
String sql = "select Balance from PlayerAccount where Player_User=@user";
comm.Parameters.Add(new OleDbParameter("user", lblUser.Text));
comm.CommandText = sql;
OleDbDataReader cursor = comm.ExecuteReader();
while (cursor.Read())
{
lblBalance.Text = cursor["Balance"].ToString();
}
con.Close();
}
}
}
嘿,抱歉,大家又问这个了,但我在过去三个小时里一直在尝试这个问题,并挥舞着白旗。仍然出现相同的错误。
我只想让从数据库中选择的余额值显示在标签中。
谢谢 ><
您没有将连接与命令对象相关联:
con.Open();
String sql = "select Balance from PlayerAccount where Player_User=@user";
OleDbCommand comm = new OleDbCommand(sql, con);
请注意,重用连接并不总是最好的设计。连接在 .NET 中汇集在一起,因此重新创建它们通常不是一项昂贵的操作。更好的设计是将 连接字符串 存储为 class 属性 然后在需要时创建连接:
private string ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\Projects\SDP\PCMS\SDP.accdb";
// or better yet - pull form app.config...
当你使用它时:
String sql = "select Balance from PlayerAccount where Player_User=@user";
using(OleDbConnection con = new OleDbConnection(ConnectionString))
{
con.Open();
using(OleDbCommand comm = new OleDbCommand(sql, con))
{
... Add parameters, execute query, return results
}
}