简单插入、更新到 SQL Server

发布于 2024-10-13 20:44:00 字数 1109 浏览 0 评论 0原文

我有这样的简单代码

 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

奈何桥上唱咆哮 2024-10-20 20:44:00

错误在于你没有太多地阅读 SQL、初学者、pagfe 1 的文档。

简单地说,我想在我的
AVUKAT表,

这不是你做的。

字符串查询 = @"更新 AVUKAT 集
穆斯特伊 = @musteriadi,AVUKAT =
avukat”;

这不是有效的 SQL。要点。 UPDATE 不是要插入的,而是要更改现有行中的值。最重要的是,您编写的内容甚至不是按 SQL 语法为 UPDATE 子句编写的,该子句具有 如果

如果您阅读错误消息,您可以在 google 的帮助下慢慢开始学习 SQL 语法,或者您只需购买一本有关 SQL 101 的书。

您不喜欢阅读错误消息,请将其发布到此处。我更倾向于提供更详细的帮助。

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

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

this is not what you do.

string query = @"UPDATE AVUKAT SET
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.

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.

橘虞初梦 2024-10-20 20:44:00

在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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文