当我想尝试检查数据库中是否有用户名时,我收到此错误,错误显示:“输入字符串格式不正确”。
以下是代码:
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());
并且错误显示:“输入字符串格式不正确”
提前致谢!
答案 0 :(得分:2)
首先,我想提示您在查询中使用参数,其次,我认为您打算在SQL中编写COUNT()
:
OleDbCommand cmd = new OleDbCommand("SELECT COUNT([Username]) FROM [Member] WHERE [Username] = ?", conn);
cmd.Parameters.Add(textBox1.Text);