我在C#中有一个使用SQL数据库的程序。我可以选择创建一个帐户并将用户信息存储在数据库中。如果我创建一个新帐户,它将起作用,并且当我要查看数据库内容时,会出现以下错误:
此数据库无法导入。它是不受支持的SQL Server版本或不受支持的兼容性。
这是我编写的用于创建新用户的代码。
try
{
if (string.IsNullOrEmpty(textBoxEmail.Text.ToString()) || string.IsNullOrEmpty(textBoxFirstName.Text.ToString()) || string.IsNullOrEmpty(textBoxLastName.Text.ToString()) || string.IsNullOrEmpty(textBoxPassword.Text.ToString()) || string.IsNullOrEmpty(textBoxUsername.Text.ToString()))
{
textBoxEmail.Text = " ";
textBoxFirstName.Text = " ";
textBoxLastName.Text = " ";
textBoxPassword.Text = " ";
textBoxUsername.Text = " ";
MessageBox.Show("Please enter a valid value", "ERROR", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
else
{
using (SqlCommand _cmd = new SqlCommand("INSERT INTO Students(username, first_name, last_name, email, password) VALUES (@user, @first, @last, @email, @pass)", _conn))
{
using (SqlCommand cmd = new SqlCommand("SELECT * FROM Students WHERE username=@user", _conn))
{
_conn.Open();
cmd.Parameters.Clear();
cmd.Parameters.AddWithValue("@user", this.textBoxUsername.Text.ToString().Trim());
cmd.ExecuteNonQuery();
rd = cmd.ExecuteReader();
if (rd.HasRows)
{
while (rd.Read())
{
MessageBox.Show("Username already exists, please use another one", "Username exists", MessageBoxButtons.RetryCancel, MessageBoxIcon.Error);
_conn.Close();
return;
}
}
rd.Close();
_conn.Close();
}
_conn.Open();
_cmd.Parameters.Clear();
_cmd.Parameters.AddWithValue("@user", this.textBoxUsername.Text.ToString().Trim());
_cmd.Parameters.AddWithValue("@first", this.textBoxFirstName.Text.ToString().Trim());
_cmd.Parameters.AddWithValue("@last", this.textBoxLastName.Text.ToString().Trim());
_cmd.Parameters.AddWithValue("@email", this.textBoxEmail.Text.ToString().Trim());
_cmd.Parameters.AddWithValue("@pass", this.textBoxPassword.Text.ToString().Trim());
_cmd.ExecuteNonQuery();
_conn.Close();
}
textBoxEmail.Text = " ";
textBoxFirstName.Text = " ";
textBoxLastName.Text = " ";
textBoxPassword.Text = " ";
textBoxUsername.Text = " ";
DialogResult res = MessageBox.Show("User created succesfully\nDo you want to login?", "User created", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
if (res == DialogResult.Yes)
{
Form1 form = new Form1();
this.Hide();
form.ShowDialog();
}
else
{
return;
}
}
}
catch(Exception ex)
{
textBoxEmail.Text = " ";
textBoxFirstName.Text = " ";
textBoxLastName.Text = " ";
textBoxPassword.Text = " ";
textBoxUsername.Text = " ";
MessageBox.Show("Error:\n" + ex, "Connection Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
请告诉我是否还有另一种查看SQL数据库内容的方法。
Here,当我点击刷新时,会出现该错误。