0

当我想尝试检查数据库中是否有可用性用户名时出现此错误,错误显示:“输入字符串格式不正确”。

这是代码:

private void CheckUsername()
{
OleDbConnection conn = new OleDbConnection();

conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=\Archives\Projects\Program\Sell System\Sell System\App_Data\db1.accdb";

conn.Open();

OleDbCommand cmd = new OleDbCommand("SELECT [Username] FROM [Member], conn);

cmd.Parameters.Add("Username", System.Data.OleDb.OleDbType.VarChar);
cmd.Parameters["Username"].Value = this.textBox1.Text;

int count = Convert.ToInt32(cmd.ExecuteScalar());

if (count != 0)
{
System.Media.SoundPlayer sound = new System.Media.SoundPlayer(@"C:\Windows\Media\Windows Notify.wav");
sound.Play();
MessageBox.Show("Username already exists! Please use another username", "Warning");
}

else
{
System.Media.SoundPlayer sound = new System.Media.SoundPlayer(@"C:\Windows\Media\Windows Notify.wav");
sound.Play();
MessageBox.Show("Username is not exists!", "Congratulations");
}
}

错误指出:int count = Convert.ToInt32(cmd.ExecuteScalar());并且错误说:“输入字符串的格式不正确”

提前致谢!

4

1 回答 1

2

首先,我想给你一个关于在查询中使用参数的提示,其次,我认为你的意思是COUNT()在你的 SQL 中编写:

OleDbCommand cmd = new OleDbCommand("SELECT COUNT([Username]) FROM [Member] WHERE [Username] = ?", conn);
cmd.Parameters.Add(textBox1.Text);
于 2013-08-25T05:22:32.593 回答