简体   繁体   中英

Insert data in SQL Database using Visual Studio 2017

I am getting error while i inserting data in to the SQL table.

"Error is System.Data.SqlClient.SqlException: 'Incorrect syntax near ')'.'

Sql Table Detail is .

USE [d]
GO
/****** Object:  Table [dbo].[data]    Script Date: 2/20/2018 6:32:54 PM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[data](
    [no] [int] NOT NULL,
    [name] [nvarchar](50) NULL,
 CONSTRAINT [PK_data] PRIMARY KEY CLUSTERED 
(
    [no] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
INSERT [dbo].[data] ([no], [name]) VALUES (10138, N'Mark')
GO
INSERT [dbo].[data] ([no], [name]) VALUES (40014, N'Antony')
GO

Form1.cs is

using System;
using System.Data;
using System.Windows.Forms;
using System.Data.SqlClient;

protected void button1_Click_1(object sender, EventArgs e)
{
  SqlConnection con = new SqlConnection();
  con.ConnectionString = ("Data Source=sPC;Initial Catalog=d;Integrated Security=True");
  con.Open();
  String st = "INSERT INTO data(no,name)";
  SqlCommand cmd = new SqlCommand(st, con);
  cmd.Parameters.AddWithValue("@no", textBox5.Text);
  cmd.Parameters.AddWithValue("@name", textBox6.Text);
  cmd.ExecuteNonQuery();
  con.Close();
}

Tried to crack the issue but luck any thing i am missing ??

This line:

String st = "INSERT INTO data(no,name)";

Must be:

String st = "INSERT INTO data(no,name) values (@no, @name)";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM