我正在尝试更新aspx页面上的数据库中的条目。我有一个aspx页面,显示具有三个属性的项目。当我单击编辑项目以更新一个或所有属性并单击更新时,我得到以下错误:
System.Data.SqlClient.SqlException:''附近的语法不正确'('。'
我的aspx代码如下:
<div class="row">
<div class="col-md-6 col-md-offset-3">
<div class="col-lg-12">
<div class="form-group alert alert-warning">
<asp:label runat="server" ID="lblNewsEdits">Please edit news details below</asp:label>
</div>
<div class="form-group">
<asp:TextBox name="txtTitle" id="txtTitle" tabindex="1" class="form-control" placeholder="Title" value="" runat="server"></asp:TextBox>
</div>
<div class="form-group">
<asp:TextBox name="txtDate" id="txtDate" tabindex="1" class="form-control" placeholder="Enter Today Date DD/MM/YYYY" value="" runat="server"></asp:TextBox>
</div>
<div class="form-group">
<asp:TextBox namee="txtNewscontent" name="txtNewscontent" id="txtNewscontent" tabindex="2" class="form-control" placeholder="News Content" runat="server"></asp:TextBox>
</div>
<div class="form-group">
<div class="row">
<div class="col-sm-6 col-sm-offset-3">
<input type="submit" name="btnEdit" id="btnEdit" tabindex="1" class="form-control btn btn-warning" value="Edit"/>
</div>
</div>
<div class="row">
<div class="col-md-1 col-md-offset-11">
<a href="/Backend/Default.aspx" class="btn btn-default pull-right">Back</a>
</div>
</div>
</div>
</div>
</div>
</div>
我的aspx.cs代码是:
protected void Page_Load(object sender, EventArgs e)
{
{
string q = Request.QueryString["id"];
int id = 0;
int.TryParse(q, out id);
if (Session["username"] == null)
Response.Redirect("Login.aspx");
if (IsPostBack)
{
EditNews(id);
Response.Redirect("~/Backend");
}
else
{
PopulateNews(id);
}
}
}
private void EditNews(int id)
{
//Create a SQL Connection - get the connection string from the web.config
SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["AppConnectionString"].ConnectionString);
//define the SQL statement you wish to run - use @ placeholders to populate parameters
string sqlStatement = "UPDATE News (Title, DatePosted, NewsContent) Values (@Title,@DatePosted,@NewsContent);";
//Set up the SQL Command
SqlCommand command = new SqlCommand(sqlStatement, connection);
//Populate the placeholders with parameter values
command.Parameters.AddWithValue("@Title", txtTitle.Text);
command.Parameters.AddWithValue("@DatePosted", Convert.ToDateTime(txtDate.Text));
command.Parameters.AddWithValue("@NewsContent", txtNewscontent.Text);
//open a connection to the database (NOTE! dont forget to close this when you are done)
connection.Open();
//Run the SQL statement against the database
command.ExecuteNonQuery();
command.Dispose();
//NOTE! close the connection
connection.Close();
}
private void PopulateNews(int id)
{
SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["AppConnectionString"].ConnectionString);
connection.Open();
string query = "SELECT Title, DatePosted, NewsContent FROM News WHERE Id = @id";
SqlCommand command = new SqlCommand(query, connection);
command.Parameters.AddWithValue("@id", id);
SqlDataReader reader = command.ExecuteReader();
while (reader.Read())
{
txtTitle.Text = reader["Title"].ToString();
txtDate.Text = reader["DatePosted"].ToString();
txtNewscontent.Text = reader["NewsContent"].ToString();
}
reader.Close();
connection.Close();
}
我的表格属性是:
CREATE TABLE [dbo].[News] (
[Id] INT IDENTITY (1, 1) NOT NULL,
[Title] NVARCHAR (100) NOT NULL,
[DatePosted] DATE NOT NULL,
[NewsContent] NTEXT NOT NULL,
[IsRead] BIT DEFAULT ((0)) NULL,
PRIMARY KEY CLUSTERED ([Id] ASC)
);
有什么想法吗?
答案 0 :(得分:1)
您的Update
声明错误。
将其替换为Insert into
。它会起作用
所以,而不是
string sqlStatement = "UPDATE News (Title, DatePosted, NewsContent) Values (@Title,@DatePosted,@NewsContent);";
您需要
string sqlStatement = "Insert into News (Title, DatePosted, NewsContent) Values (@Title,@DatePosted,@NewsContent);";
如果你真的需要update
查询它们,它的语法是不同的
Update TableName Set Field1=@value1, … Where ConditionHere
答案 1 :(得分:0)
更新语句的语法不正确。
UPDATE News (Title, DatePosted, NewsContent) Values (@Title,@DatePosted,@NewsContent);
应该是
UPDATE News
SET Title = @Title, DatePosted = @DatePosted, NewsContent = @NewsContent
WHERE -- some discriminator here like match on ID
如果你打算做一个instert(添加),那么语法应该是
INSERT INTO News (Title, DatePosted, NewsContent) Values (@Title,@DatePosted,@NewsContent);
另外一项建议。如果类型实现接口IDisposable
,则最佳做法是将该实例包装在using
块中,以确保始终释放外部资源。 SqlConnection
和SqlCommand
以及SqlDataReader
都实施IDisposable
。例如:
using(SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["AppConnectionString"].ConnectionString)
{
// rest of code here
}
现在,一旦退出块,连接总是被关闭并处理,即使抛出Exception
。