1

我正在尝试将选项卡控件中的一组文本框中的值存储到选项卡控件中另一个选项卡上的数据网格视图中,但是代码会引发异常,并显示以下消息“'系统'附近的语法不正确”。

这是我的代码导致错误

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Windows_8_Login
{
    public partial class Stock : Form
    {
        public Stock()
        {
            InitializeComponent();
        }

        private void Stock_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'loginScreenDataSet7.Stock' table. You can move, or remove it, as needed.
            this.stockTableAdapter1.Fill(this.loginScreenDataSet7.Stock);


        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
            Form dash = new DashBoard();
            dash.Show();

        }



        private void GenButton_Click(object sender, EventArgs e)
        {
            Random Gen = new Random();
            ProdID.Text = Convert.ToString(Gen.Next(1000000, 9000000));
        }

        private void SaveBtn_Click(object sender, EventArgs e)
        {
            try
           {
                SqlConnection sc = new SqlConnection();
                SqlCommand com = new SqlCommand();
                sc.ConnectionString = ("Data Source=localhost;Initial Catalog=LoginScreen;Integrated Security=True");
                sc.Open();
                com.Connection = sc;
                com.CommandText = ("INSERT INTO Stock (Prod_ID, Prod_Name, Prod_Cat, Supplier, Cost, Price_1, Price_2, Price_3) VALUES (" + ProdID.Text + "" + ProdName.Text + "" + ProdCat.Text + "" + ProdSup.Text + "" + ProdCost.Text + "" + ProdPrice1.Text + "" + ProdPrice2 + "" + ProdPrice3.Text + "");
                com.ExecuteNonQuery();
                sc.Close();
           }
            catch (Exception x)
            {
               MessageBox.Show(x.Message);
            }
        }
    }
}
4

1 回答 1

0

您的 com.CommandText 包含一些错误...调试它并获取值!:-) 你没有放在','变量之间,你忘了关闭括号......

像那样 :

com.CommandText = (
    "INSERT INTO Stock "
  + "(Prod_ID, Prod_Name, Prod_Cat, Supplier, Cost, Price_1, Price_2, Price_3) "
  + "VALUES ('" 
  + ProdID.Text + "','" + ProdName.Text + "','" + ProdCat.Text + "','" 
  + ProdSup.Text + "','" + ProdCost.Text + "','" + ProdPrice1.Text + "','"
  + ProdPrice2.Text + "','" + ProdPrice3.Text
  + ")");

但是,您应该使用 SqlParameters。(和换行符)

于 2013-04-17T14:58:11.370 回答