保存前搜索记录

时间:2014-07-21 06:16:41

标签: c# sql

我正在开发一个使用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
    }
}

1 个答案:

答案 0 :(得分:2)

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

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

看看this thread,它询问同样的问题。