简单的插入,更新到SQL Server。

时间:2022-03-26 15:43:41

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,

错误在哪里?简单地说,我想在AVUKAT表中添加两列,

How can i solve it?

我怎么解决呢?

2 个解决方案

#1


2  

The error is that you dont read the documentation for SQL, beginners, pagfe 1 pretty much.

错误之处在于,您几乎没有阅读SQL、初学者和pagfe 1的文档。

Simply, i want to add two column in my AVUKAT table,

简单地说,我想在AVUKAT表中添加两列,

this is not what you do.

这不是你做的。

string query = @"UPDATE AVUKAT SET MUSTERİ = @musteriadi, AVUKAT = avukat";

字符串查询= @”更新AVUKAT组MUSTERİ= @musteriadi AVUKAT = AVUKAT”;

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.

这不是有效的SQL。点。更新不是插入的地方,而是在现有行中更改值的地方。最重要的是,您所编写的甚至不是针对具有不同语法的UPDATE子句的每个SQL语法。

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.

如果您阅读了错误消息,您可以在谷歌的帮助下慢慢开始学习SQL语法。或者你可能会得到一本关于SQL 101的书。

If you dont like readingthe error message, posting it here would make me more inclined to offer more detailed help.

如果你不喜欢阅读错误信息,在这里发布它将使我更倾向于提供更详细的帮助。

#2


1  

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.

在c#代码中,如果想使用@musteriadi,需要创建一个SqlParameter。此外,您不能像这样引用avukat变量。请看一些如何使用SQLCommand和sql语法的示例。

#1


2  

The error is that you dont read the documentation for SQL, beginners, pagfe 1 pretty much.

错误之处在于,您几乎没有阅读SQL、初学者和pagfe 1的文档。

Simply, i want to add two column in my AVUKAT table,

简单地说,我想在AVUKAT表中添加两列,

this is not what you do.

这不是你做的。

string query = @"UPDATE AVUKAT SET MUSTERİ = @musteriadi, AVUKAT = avukat";

字符串查询= @”更新AVUKAT组MUSTERİ= @musteriadi AVUKAT = AVUKAT”;

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.

这不是有效的SQL。点。更新不是插入的地方,而是在现有行中更改值的地方。最重要的是,您所编写的甚至不是针对具有不同语法的UPDATE子句的每个SQL语法。

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.

如果您阅读了错误消息,您可以在谷歌的帮助下慢慢开始学习SQL语法。或者你可能会得到一本关于SQL 101的书。

If you dont like readingthe error message, posting it here would make me more inclined to offer more detailed help.

如果你不喜欢阅读错误信息,在这里发布它将使我更倾向于提供更详细的帮助。

#2


1  

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.

在c#代码中,如果想使用@musteriadi,需要创建一个SqlParameter。此外,您不能像这样引用avukat变量。请看一些如何使用SQLCommand和sql语法的示例。