i'm new to AsP.net when i'm trying my usual code to connect to database there's exception keep shown and idon't know what's wrong
the exception is :
当我尝试我的常规代码连接数据库时,我对AsP.net是陌生的,有一个异常保持显示,我不知道什么是错误的异常是:
" System.Data.SqlClient.SqlException: Incorrect syntax near the keyword 'file'."
and this my code :
我的密码是
string ID = Request.QueryString["id"];
SqlCommand cmd = new SqlCommand("select title,file path,Upload Date from [Media] where ID=@id", conn);
cmd.CommandType = CommandType.Text;
SqlDataReader rdr=null;
try
{
conn.Open();
rdr = cmd.ExecuteReader();
try
{
conn.Open();
rdr = cmd.ExecuteReader();
// print the CustomerID of each record
while (rdr.Read())
{
pathTextBox.Text = rdr["file Path"].ToString();
DateTextBox.Text = rdr["Upload Date"].ToString();
titleTextBox.Text = rdr["title"].ToString();
}
Image1.ImageUrl = pathTextBox.Text;
}
2 个解决方案
#1
1
if you have spaces in column names you need to use brackets like below
如果列名中有空格,则需要使用括号,如下所示
select title,[file path],[Upload Date] from [Media] where ID=@id
选择title,[file path],[Upload Date] from [Media],其中ID=@id
using (var conn = new SqlConnection(SomeConnectionString))
using (var cmd = conn.CreateCommand())
{
conn.Open();
cmd.CommandText = "select title,[file path],[Upload Date] from [Media] where ID=@id";
cmd.Parameters.AddWithValue("@id", idval); // set the id parameter
using (var reader = cmd.ExecuteReader())
{
if (reader.Read()) // you don't need while loop
{
pathTextBox.Text = reader.GetString(reader.GetOrdinal("[file path]"))
}
}
}
#2
1
If your column names contains white space (which is not recomended) you should use square brackets like []
. For example, [Upload Date]
如果您的列名包含空格(不返回),则应该使用方括号,如[]。例如,(上传日期)
Column names must follow the rules for identifiers.
列名必须遵循标识符的规则。
From Database Identifiers
从数据库标识符
SELECT *
FROM [My Table] --Identifier contains a space and uses a reserved keyword.
WHERE [order] = 10 --Identifier is a reserved keyword.
That's why you should use them like;
这就是为什么你应该像;
select title,[file path],[Upload Date]
Also you don't add your parameter value anywhere in your code.
此外,在代码的任何地方都不添加参数值。
SqlCommand cmd = new SqlCommand("select title,[file path],[Upload Date] from Media where ID=@id", conn);
cmd.Parameters.AddWithValue("@id", YourIDValue);
Also use using
statement to dispose your SqlConnection
and SqlCommand
.
还可以使用using语句来配置SqlConnection和SqlCommand。
using (SqlConnection conn = new SqlConnection(YourConnectionString))
{
using (SqlCommand cmd = new SqlCommand())
{
//Your code..
}
}
#1
1
if you have spaces in column names you need to use brackets like below
如果列名中有空格,则需要使用括号,如下所示
select title,[file path],[Upload Date] from [Media] where ID=@id
选择title,[file path],[Upload Date] from [Media],其中ID=@id
using (var conn = new SqlConnection(SomeConnectionString))
using (var cmd = conn.CreateCommand())
{
conn.Open();
cmd.CommandText = "select title,[file path],[Upload Date] from [Media] where ID=@id";
cmd.Parameters.AddWithValue("@id", idval); // set the id parameter
using (var reader = cmd.ExecuteReader())
{
if (reader.Read()) // you don't need while loop
{
pathTextBox.Text = reader.GetString(reader.GetOrdinal("[file path]"))
}
}
}
#2
1
If your column names contains white space (which is not recomended) you should use square brackets like []
. For example, [Upload Date]
如果您的列名包含空格(不返回),则应该使用方括号,如[]。例如,(上传日期)
Column names must follow the rules for identifiers.
列名必须遵循标识符的规则。
From Database Identifiers
从数据库标识符
SELECT *
FROM [My Table] --Identifier contains a space and uses a reserved keyword.
WHERE [order] = 10 --Identifier is a reserved keyword.
That's why you should use them like;
这就是为什么你应该像;
select title,[file path],[Upload Date]
Also you don't add your parameter value anywhere in your code.
此外,在代码的任何地方都不添加参数值。
SqlCommand cmd = new SqlCommand("select title,[file path],[Upload Date] from Media where ID=@id", conn);
cmd.Parameters.AddWithValue("@id", YourIDValue);
Also use using
statement to dispose your SqlConnection
and SqlCommand
.
还可以使用using语句来配置SqlConnection和SqlCommand。
using (SqlConnection conn = new SqlConnection(YourConnectionString))
{
using (SqlCommand cmd = new SqlCommand())
{
//Your code..
}
}