读书人

C#连接MYSQL有关问题帮忙看一下

发布时间: 2012-05-27 05:42:30 作者: rapoo

C#连接MYSQL问题,帮忙看一下。
MySQLConnection conn = null;

conn = new MySQLConnection(new MySQLConnectionString("localhost", "bishe", "root", "sa").AsString);

conn.Open();

string h11 = "select * from table1 where id = '"+uid+"'";
MySQLCommand commn = new MySQLCommand(h11, conn);

int count = (int)commn.ExecuteScalar();
if (count == 1)
{
MessageBox.Show("存在该账号!");
}
else
{
MessageBox.Show("不存在该账号。");
this.Close();
}
commn.ExecuteNonQuery();



出现问题:
int count = (int)commn.ExecuteScalar();未将对象引用设置到对象的实例。
如何解决啊。

[解决办法]
不存在时,没有存在的行,所以你是取不到数据的,也没所谓转化为int,
[解决办法]
int count = int.Parse(commn.ExecuteScalar().Tostring());

读书人网 >C#

热点推荐