Я пытаюсь проверить, было ли введенное имя пользователя уже в таблице базы данных Table в столбце с именем name. Если он еще не был использован, он может быть введен в таблицу, я проверил seperatle вставки, и он отлично работает.
protected void Unnamed1_Click(object sender, EventArgs e)
{
string name = tbUser.Text;
string pass = tbPass.Text;
using (SqlConnection connection = new SqlConnection(conString))
{
connection.Open();
string checkUser = "select Count(*) from dbo.Table where name = '"+name+"'";
SqlCommand command = new SqlCommand(checkUser, connection);
int temp = Convert.ToInt32(command.ExecuteScalar().ToString());
if (temp == 1)
{
lblError.Text = "Username Taken";
}
else
{
command.Connection = connection;
command.CommandType = System.Data.CommandType.Text;
command.CommandText = "INSERT INTO [dbo].[Table] VALUES(@name, @pass)";
command.Parameters.Add("@name", System.Data.SqlDbType.NVarChar, 50).Value = name;
command.Parameters.Add("@pass", System.Data.SqlDbType.NVarChar, 50).Value = GetMd5Hash(pass);
int rowsAffected = command.ExecuteNonQuery();
}
}
}
error я get is, исключение типа "System.Data.SqlClient.SqlException" произошло в System.Data.dll, но не было обработано в коде пользователя
Дополнительная информация: Неправильный синтаксис рядом с ключевым словом "Таблица".
Ошибка возникает в этой строке.
string checkUser = "select Count(*) from dbo.Table where name = '"+name+"'";
Решение:
Вам нужно поместить таблицу в [].
string checkUser = "select Count(*) from [dbo].[Table] where name = '"+name+"'";
Информация:
"Reserved keywords should not be used as object names. Databases upgraded from
earlier versions of SQL Server may contain identifiers that include words not reserved
in the earlier version, but that are reserved words for the current version of SQL Server.
You can refer to the object by using delimited identifiers until the name can be
changed." http://msdn.microsoft.com/en-us/library/ms176027.aspx
а также
"If your database does contain names that match reserved keywords, you must
use delimited identifiers when you refer to those objects. For more information,
see Identifiers (DMX)." http://msdn.microsoft.com/en-us/library/ms132178.aspx