@keknyx

Как выполнить sql запрос кнопкой?

sql запрос и он выполнился по нажатию кнопки:
spoiler
private void button3_Click(object sender, EventArgs e)
        {
            SqlCommand command = new SqlCommand(
                $"INSERT INTO [Postavki] (Name_Of_Product, Price, CapacityCBM, Provider, ProviderAddress, ProviderPhone, ProviderEmail, Date_Of_Delivery, Quantity, Number_Of_Contract) VALUES ((SELECT TOP 1 Name_Of_Product FROM Postavki ORDER BY NEWID()), (SELECT TOP 1 Price FROM Postavki ORDER BY NEWID()), (SELECT TOP 1 CapacityCBM FROM Postavki ORDER BY NEWID()), (SELECT TOP 1 Provider FROM Postavki ORDER BY NEWID()), (SELECT TOP 1 ProviderAddress FROM Postavki ORDER BY NEWID()), (SELECT TOP 1 ProviderPhone FROM Postavki ORDER BY NEWID()), (SELECT TOP 1 ProviderEmail FROM Postavki ORDER BY NEWID()), (SELECT TOP 1 Date_Of_Delivery FROM Postavki ORDER BY NEWID()), (SELECT TOP 1 Quantity FROM Postavki ORDER BY NEWID()), (SELECT TOP 1 Number_Of_Contract FROM Postavki ORDER BY NEWID())", sqlConnection);
           command.ExecuteNonQuery();
        }


ошибка:
System.Data.SqlClient.SqlException: "Incorrect syntax near ')'.

в чем проблема?

Весь код формы:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Configuration;
using System.Data.SqlClient;

namespace SQL_DB
{
    public partial class Form1 : Form
    {

        private SqlConnection sqlConnection = null;

        private SqlDataAdapter adapter = null;

        private DataTable table = null;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            sqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["KeyDB"].ConnectionString);

            sqlConnection.Open();

            adapter = new SqlDataAdapter("SELECT * FROM Postavki", sqlConnection);

            table = new DataTable();

            adapter.Fill(table);

            dataGridView1.DataSource = table;

            if (sqlConnection.State == ConnectionState.Open)
            {
                MessageBox.Show("Подключение установлено");
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SqlCommand command = new SqlCommand(
                $"INSERT INTO [Postavki] (Name_Of_Product, Price, CapacityCBM, Provider, ProviderAddress, ProviderPhone, ProviderEmail, Date_Of_Delivery, Quantity, Number_Of_Contract) VALUES (@Name_Of_Product, @Price, @CapacityCBM, @Provider, @ProviderAddress, @ProviderPhone, @ProviderEmail, @Date_Of_Delivery, @Quantity, @Number_Of_Contract)",
                sqlConnection);

            DateTime date = DateTime.Parse(textBox8.Text);

            command.Parameters.AddWithValue("Name_Of_Product", textBox1.Text);
            command.Parameters.AddWithValue("Price", textBox2.Text);
            command.Parameters.AddWithValue("CapacityCBM", textBox3.Text);
            command.Parameters.AddWithValue("Provider", textBox4.Text);
            command.Parameters.AddWithValue("ProviderAddress", textBox5.Text);
            command.Parameters.AddWithValue("ProviderPhone", textBox6.Text);
            command.Parameters.AddWithValue("ProviderEmail", textBox7.Text);
            command.Parameters.AddWithValue("Date_Of_Delivery", $"{date.Month}/{date.Day}/{date.Year}");
            command.Parameters.AddWithValue("Quantity", textBox9.Text);
            command.Parameters.AddWithValue("Number_Of_Contract", textBox10.Text);

            MessageBox.Show(command.ExecuteNonQuery().ToString());
        }

        private void button2_Click(object sender, EventArgs e)
        {
            table.Clear();

            adapter.Fill(table);

            dataGridView1.DataSource = table;



        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

       

        

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {
            SqlCommand command = new SqlCommand(
               $"INSERT INTO [Postavki] (Name_Of_Product, Price, CapacityCBM, Provider, ProviderAddress, ProviderPhone, ProviderEmail, Date_Of_Delivery, Quantity, Number_Of_Contract)" + "VALUES ((SELECT TOP 1 Name_Of_Product FROM Postavki ORDER BY NEWID()), (SELECT TOP 1 Price FROM Postavki ORDER BY NEWID()), (SELECT TOP 1 CapacityCBM FROM Postavki ORDER BY NEWID()), (SELECT TOP 1 Provider FROM Postavki ORDER BY NEWID()), (SELECT TOP 1 ProviderAddress FROM Postavki ORDER BY NEWID()), (SELECT TOP 1 ProviderPhone FROM Postavki ORDER BY NEWID()), (SELECT TOP 1 ProviderEmail FROM Postavki ORDER BY NEWID()), (SELECT TOP 1 Date_Of_Delivery FROM Postavki ORDER BY NEWID()), (SELECT TOP 1 Quantity FROM Postavki ORDER BY NEWID()), (SELECT TOP 1 Number_Of_Contract FROM Postavki ORDER BY NEWID())", sqlConnection);
            command.ExecuteNonQuery();
        }
  • Вопрос задан
  • 150 просмотров
Решения вопроса 1
hint000
@hint000
у админа три руки
System.Data.SqlClient.SqlException: "Incorrect syntax near ')'."

По-моему, он намекает, что у вас не хватает одной скобки в конце запроса:
...ORDER BY NEWID())", sqlConnection);
Вот разве не так нужно?:
...ORDER BY NEWID()))", sqlConnection);
Ответ написан
Пригласить эксперта
Ответы на вопрос 1
Shellon
@Shellon
Он намекает просто на скобку. А почему вы сортируете по NEWID() , а не просто NEWID?
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы