开发者

Simple Insert, Update to SQL Server

开发者 https://www.devze.com 2023-02-06 04:51 出处:网络
I have simple code like this using System; using System.Collections.Generic; using System.Linq; using System.Web;

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 = n开发者_开发问答ew 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?


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.


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号