C# 在保存记录之前先搜索它

C# 在保存记录之前先搜索它,c#,sql,C#,Sql,我正在开发一个简单的清单程序,使用C#sql存储硬件设备 在保存记录之前的程序中,我希望在保存之前搜索序列号(如果序列号存在),或将其添加到记录中以避免重复,我收到此异常错误: 语法错误:“否”运算符后缺少操作数 下面是我的代码 using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq

我正在开发一个简单的清单程序,使用C#sql存储硬件设备 在保存记录之前的程序中,我希望在保存之前搜索序列号(如果序列号存在),或将其添加到记录中以避免重复,我收到此异常错误:

语法错误:“否”运算符后缺少操作数

下面是我的代码

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.Data.SqlClient;
using  System.Data.SqlServerCe;

namespace DataBaseApplication
{
    public partial class Form1 : Form
    {
        #region Fields
        SqlDataAdapter dataAdapter;
        DataSet ds;
        // DataRowView drView;
        CurrencyManager crmng;
        SqlConnection con;
        ToolTip tootip;
        int inc = 0;
        #endregion

        public Form1()
        {
            InitializeComponent();
        }



        #region Connetion to the DataBase and fill the DataSet Table

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'impiDbDataSet1.Impi' table. You can move, or remove it, as needed.
            //this.impiTableAdapter1.Fill(this.impiDbDataSet1.Impi);
            // TODO: This line of code loads data into the 'impiDbDataSet.Impi' table. You can move, or remove it, as needed.
            //this.impiTableAdapter.Fill(this.impiDbDataSet.Impi);
            try
            {
                string conStrings = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\ImpiDb.mdf;Integrated Security=True;Connect Timeout=30";
                string sql = "Select * from Impi";
                con = new SqlConnection(conStrings);
                con.Open();
                dataAdapter = new SqlDataAdapter(sql, con);
                ds = new DataSet();
                dataAdapter.Fill(ds, "Impi");
                impdg.DataSource = ds.Tables["Impi"].DefaultView;
                crmng = (CurrencyManager)impdg.BindingContext[ds.Tables[0]];
                tootip = new ToolTip();


                con.Close();
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message);
            }

        }
        #endregion
        #region Save the Records to the DataBase

        private void btnSave_Click(object sender, EventArgs e)
        {

            try
            {
                System.Data.SqlClient.SqlCommandBuilder cb;
                cb = new System.Data.SqlClient.SqlCommandBuilder(dataAdapter);
               // cb.DataAdapter.Update(ds.Tables["Impi"]);

                DataRow dr = ds.Tables["Impi"].NewRow();
                dr[0] = txtSerial.Text;


                if (txtName.Text != "")
                {

                    dr[1] = txtName.Text;
                    //Busy trying to solve exception errors and saving the record functionality without duplicating primary key
                }
                if (cbModel.Text == "MK1" || cbModel.Text == "MK2")
                {
                    dr[2] = cbModel.Text;
                }


                if (cbStatus.Text == "Serviceble" || cbStatus.Text == "Unserviceble")
                {
                    dr[3] = cbStatus.Text;
                }
                if (cbDeprtmnt.Text == "AIR" || cbDeprtmnt.Text == "LAND" || cbDeprtmnt.Text == "NAVY" || cbDeprtmnt.Text == "SPECIAL FORCE")
                {


                    dr[4] = cbDeprtmnt.Text;
                }

                //  ds.Tables["Impi"].Rows.Add(dr);

                //This is where i stopped trying to figure out how to save my records properly

                if (txtSerial.Text.Length!=0)
                {
                    bool search = SearchSerialNumberBeforeSave(txtSerial.Text);
                    if (search == false)
                    {
                        DialogResult dr2 = MessageBox.Show("Are you sure  you want to save this serial number", "Message", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                        if (dr2 == DialogResult.Yes)
                        {
                            ds.Tables["Impi"].Rows.Add(dr);
                            dataAdapter.Update(ds, "Impi");


                            MessageBox.Show("Serial Number Added Successful");
                        }

                        // System.Data.SqlClient.SqlCommandBuilder cb;
                        //cb = new System.Data.SqlClient.SqlCommandBuilder(dataAdapter);
                    }                               //cb.DataAdapter.Update(ds.Tables["Impi"]);
                    else
                    {

                        MessageBox.Show("This Serial Number Exist and will create the duplicate.\nSerial Number not Saved");
                        MessageBox.Show("Data Entry was not saved", "Sorry", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
                else
                {
                    MessageBox.Show("Please Enter a Impi Serial Number","Data Entry");
                    txtSerial.Text = "Please Enter Impi Serial Number";
                    txtSerial.ForeColor = Color.Red;
                    tootip.SetToolTip(txtSerial, txtSerial.Text);
                }

                // crmng.Position += 1;
                // inc = crmng.Position - 1;
               // btnAdd.Enabled = true;
               // btnSave.Enabled = false;


                if (txtName.Text.Length==0)
                {
                    txtName.Text = "Please Enter the Track Number";
                    txtName.ForeColor = Color.Red;
                    tootip.SetToolTip(txtName, txtName.Text);
                }

                if (cbModel.Text.Length == 0)
                {
                    cbModel.Text = "Please Select the Model Of the device";
                    cbModel.ForeColor = Color.Red;
                    tootip.SetToolTip(cbModel, cbModel.Text);
                }

                if (cbStatus.Text.Length == 0)
                {
                    cbStatus.Text = "Please Select the status of the device";
                    cbStatus.ForeColor = Color.Red;
                    tootip.SetToolTip(cbStatus, cbStatus.Text);
                }

                if (cbDeprtmnt.Text.Length == 0)
                {
                    cbDeprtmnt.Text = "Please Select the assigned department";
                    cbDeprtmnt.ForeColor = Color.Red;
                }
                else
                {
                    MessageBox.Show("Data Entry was not Saved", "Sorry", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            }
            catch (SqlException ex)
            {
                MessageBox.Show(ex.Message);
            }

            con.Close();

        }


        #endregion

        #region Search Method

        public bool SearchSerialNumberBeforeSave(String name)
        {
            int result = 0;
            DataRow[] retRows;
            bool val;
            //This line of code give this exception error Syntax error: Missing operand after 'No' operator.
            retRows = ds.Tables["Impi"].Select("Serial No='" + name + "'");

            result = retRows.Length;
            if (result > 0)
            {
                val = true;
            }

            else
            {
                val = false;
            }

            return val;
        }

        #endregion
    }
}

序列号中的空格导致了此问题。 要解决此问题,请将
序列号
替换为
[Serial No]

retRows = ds.Tables["Impi"].Select("[Serial No]='" + name + "'");

看一看,它询问了相同的问题。

在哪一行出现此错误?你调试代码了吗<代码>retRows=ds.Tables[“Impi”]。选择(“[序列号]=”+“名称+”)”这个答案可以帮助我在添加方括号后使程序正常工作