我得到這個錯誤,當我想嘗試檢查數據庫中是否有可用性用戶名時,錯誤說:「輸入字符串格式不正確」。檢查用戶名可用性錯誤
下面是代碼:中指出
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());
和錯誤說:「輸入字符串是格式不正確」
提前感謝!
請開始使用參數化查詢。這種方法是一個等待發生的sql注入災難 – TGH
用戶名是一個整數嗎? – dcaswell
@TGH:謝謝,我會做的! – Reinhardt