保存记录前进行搜索

本文关键字:搜索 记录 保存 | 更新日期: 2023-09-27 18:13:42

我正在开发一个简单的库存程序,使用c# sql存储硬件设备在我的程序中,在保存记录之前,我想在保存或将其添加到记录中以避免重复之前搜索序列号是否存在,并且我收到以下异常错误:

语法错误:'No'操作符后缺少操作数

下面是我的代码。

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中的空格导致此问题。为了解决这个问题,用[Serial No]代替Serial No

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

看看这个线程,它问同样的问题。