简单插入、更新到 SQL Server
我有这样的简单代码
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
protected void Add_Click(object sender, EventArgs e)
{
string strConnectionString = ConfigurationManager.ConnectionStrings["SqlServerCstr"].ConnectionString;
SqlConnection myConnection = new SqlConnection(strConnectionString);
string musteriadi = DropDownList1.SelectedIndex.ToString();
string avukat = DropDownList2.SelectedIndex.ToString();
string query = @"UPDATE AVUKAT SET MUSTERİ = @musteriadi, AVUKAT = avukat";
SqlCommand myCommand = new SqlCommand();
myCommand.Connection = myConnection;
myConnection.Open();
GridView1.DataSource = myCommand.ExecuteReader();
GridView1.DataBind();
GridView1.Visible = true;
myConnection.Close();
}
错误在哪里?简而言之,我想在 AVUKAT 表中添加两列,
我该如何解决它?
I have simple code like this
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
protected void Add_Click(object sender, EventArgs e)
{
string strConnectionString = ConfigurationManager.ConnectionStrings["SqlServerCstr"].ConnectionString;
SqlConnection myConnection = new SqlConnection(strConnectionString);
string musteriadi = DropDownList1.SelectedIndex.ToString();
string avukat = DropDownList2.SelectedIndex.ToString();
string query = @"UPDATE AVUKAT SET MUSTERİ = @musteriadi, AVUKAT = avukat";
SqlCommand myCommand = new SqlCommand();
myCommand.Connection = myConnection;
myConnection.Open();
GridView1.DataSource = myCommand.ExecuteReader();
GridView1.DataBind();
GridView1.Visible = true;
myConnection.Close();
}
Where is the error? Simply, i want to add two column in my AVUKAT table,
How can i solve it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
错误在于你没有太多地阅读 SQL、初学者、pagfe 1 的文档。
这不是你做的。
这不是有效的 SQL。要点。 UPDATE 不是要插入的,而是要更改现有行中的值。最重要的是,您编写的内容甚至不是按 SQL 语法为 UPDATE 子句编写的,该子句具有 如果
如果您阅读错误消息,您可以在 google 的帮助下慢慢开始学习 SQL 语法,或者您只需购买一本有关 SQL 101 的书。
您不喜欢阅读错误消息,请将其发布到此处。我更倾向于提供更详细的帮助。
The error is that you dont read the documentation for SQL, beginners, pagfe 1 pretty much.
this is not what you do.
This is not valid SQL. Point. UPDATE is not ther to insert, it is there to CHANGE VALUES IN EXISTING ROWS. On top of that, what you write is not even vlaid per SQL syntax for an UPDATE clause which has a different syntax.
If you read the error messae you can slowly start - with the help of google - to learn the syntax of SQL. Or you jsut get a book on SQL 101.
If you dont like readingthe error message, posting it here would make me more inclined to offer more detailed help.
在C#代码中,如果要使用@musteriadi,则需要创建一个SqlParameter。此外,您不能像您那样引用 avukat 变量。查看一些有关如何使用 SQLCommand 以及 sql 语法的示例。
In the C# code, you need to create a SqlParameter if you want to use @musteriadi. In addition you cannot refer to the avukat variable as you do. Take a look at some examples of how to use the SQLCommand as well as the sql syntax.