System.Data.SqlServerCe.dll中发生未处理的“System.Data.SqlServerCe.SqlCeException”类型异常

时间:2017-10-11 09:34:52

标签: c# exception unhandled sql-query-store

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlServerCe;

namespace StaffFiles1._0
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        private void fikeToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Application.Exit();

        }



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

        }


        private void toolStripButton1_Click(object sender, EventArgs e)  //new record button
        {
            NewRecordMenuItem.Enabled = false;
            AddMenuButton.Enabled = true;
            DeleteMenuButton.Enabled = true;
            SaveMenuButton.Enabled = true;

            EmployeeGroupBox.Enabled = true;
            GenInfoGroupBox.Enabled = true;

            name_TextBox.Focus();
        }

        private void empoyee_GIListBox_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void empoyee_GIListBox_SelectedIndexChanged_1(object sender, EventArgs e)
        {
            if (EmpListBox.SelectedIndex == -1)
            {
                GenInfoGroupBox.Enabled = false;
                EmployeeGroupBox.Enabled = false;
                AddMenuButton.Enabled = false;
                DeleteMenuButton.Enabled = false;

                NewRecordMenuItem.Enabled = true;

            }
        }

        private void AddMenuButton_Click(object sender, EventArgs e)
        {


            if (IsValidated())
            { 
            SqlCeConnection con = new SqlCeConnection(@"Data Source=c:\users\the doom's day\documents\visual studio 2012\Projects\StaffFiles1.0\StaffFiles1.0\Employee_DB.sdf");
            SqlCeCommand cmd = new SqlCeCommand("INSERT INTO Empoyee_GI(Name,Father Name, Birthdate, Address, City, Zone, Province, Cell Number, Email, Employement Status, Hire Date, Renewal Date, Location, Position, BPS, Department, Gender, Maritial Status, CNIC, Employement Number)VALUES('"+name_TextBox.Text+"','"+father_Name_TextBox.Text+"','"+birthdate_DateTimePicker.Text+"','"+address_TextBox.Text+"','"+city_ComboBox.Text+"','"+zone_ComboBox.Text+"','"+province_ComboBox.Text+"','"+cell_Number_TextBox.Text+"','"+email_TextBox.Text+"','"+employement_Status_ComboBox.Text+"','"+hire_Date_DateTimePicker.Text+"','"+renewal_Date_DateTimePicker.Text+"','"+location_ComboBox.Text+"','"+position_ComboBox.Text+"','"+bPS_ComboBox.Text+"','"+department_ComboBox.Text+"','"+gender_ComboBox.Text+"','"+maritial_Status_ComboBox.Text+"','"+cNICTextBox.Text+"','"+employement_Number_TextBox.Text+"') ",con);
            con.Open();

            cmd.ExecuteNonQuery();

            con.Close();
            MessageBox.Show("Record Added", "Working", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);


            }



        }

        private bool IsValidated()
        {
            Regex CN = new Regex(@"^[0-9]{5}-[0-9]{7}-[0-9]{1}$");
            Regex CL = new Regex(@"^[0-9]{4}-[0-9]{7}$");
            Regex EM = new Regex(@"^[a-zA-Z0-9]{1,20}@[a-zA-Z0-9]{1,20}.[a-zA-Z]{2,3}$");

            if (name_TextBox.Text.Trim() == string.Empty)
            {
                MessageBox.Show("Name Field Required", "Empty Field", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                name_TextBox.Focus();
                return false;
            }
            if (father_Name_TextBox.Text.Trim() == string.Empty)
            {
                MessageBox.Show("Father Name Field Required", "Empty Field", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
               father_Name_TextBox.Focus();
                return false;
            }
            if (cNICTextBox.Text.Trim() == string.Empty)
            {
                MessageBox.Show("CNIC Field Required", "Empty Field", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                cNICTextBox.Focus();
                return false;

            }
            if (!CN.IsMatch(cNICTextBox.Text))
            {
                MessageBox.Show("CNIC Format should be like 11111-1111111-1", "Format Mismatch", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                cNICTextBox.Clear();
                cNICTextBox.Focus();
                return false;

            }
            if (cell_Number_TextBox.Text != string.Empty)
            {
                if (!CL.IsMatch(cell_Number_TextBox.Text))
                {
                    MessageBox.Show("Cell Number Format should be like 0300-1234567", "Format Mismatch", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    cell_Number_TextBox.Clear();
                    cell_Number_TextBox.Focus();
                    return false;

                }
            }
            if (email_TextBox.Text != string.Empty)
            {
                if (!EM.IsMatch(email_TextBox.Text))
                {
                    MessageBox.Show("Email Format should be like someone@someone.com", "Format Mismatch", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                    email_TextBox.Clear();
                    email_TextBox.Focus();
                    return false;

                }

            }


            if (address_TextBox.Text.Trim() == string.Empty)
            {
                MessageBox.Show("Address Field Required", "Empty Field", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
                address_TextBox.Focus();
                return false;
            }
            if (city_ComboBox.Text.Trim() == string.Empty)
            {
                MessageBox.Show("City field Required", "Empty Field", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

                return false;
            }
            if (province_ComboBox.Text.Trim() == string.Empty)
            {
                MessageBox.Show("Province field Required", "Empty Field", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

                return false;
            }

            if (employement_Status_ComboBox.Text.Trim() == string.Empty)
            {
                MessageBox.Show("Status Field Required", "Empty Field", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

                return false;
            }
            if (location_ComboBox.Text.Trim() == string.Empty)
            {
                MessageBox.Show("Location Field Required", "Empty Field", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

                return false;
            }
            if (position_ComboBox.Text.Trim() == string.Empty)
            {
                MessageBox.Show("Position Field Required", "Empty Field", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

                return false;
            }
            if (bPS_ComboBox.Text.Trim() == string.Empty)
            {
                MessageBox.Show("BPS Field Required", "Empty Field", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

                return false;
            }
            if (department_ComboBox.Text.Trim() == string.Empty)
            {
                MessageBox.Show("Department Field Required", "Empty Field", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

                return false;
            }
            if (gender_ComboBox.Text.Trim() == string.Empty)
            {
                MessageBox.Show("Gender Field Required", "Empty Field", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

                return false;
            }
            if (maritial_Status_ComboBox.Text.Trim() == string.Empty)
            {
                MessageBox.Show("Maritial Status Field Required", "Empty Field", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);

                return false;
            }


            return true;

        }

        private void DeleteMenuButton_Click(object sender, EventArgs e)
        {

        }

        private void SaveMenuButton_Click(object sender, EventArgs e)
        {

        }

        private void city_ComboBox_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

        private void saveToolStripMenuItem_Click(object sender, EventArgs e)
        {

        }

        private void tabPage1_Click(object sender, EventArgs e)
        {

        }
    }
}

当我运行代码时,我遇到了这个错误,我怎么能解决这个问题呢?

  

未处理的类型异常   发生'System.Data.SqlServerCe.SqlCeException'   System.Data.SqlServerCe.dll c#

当我按添加记录按钮添加记录时,会出现此错误 紧凑的本地数据库中的字段。

1 个答案:

答案 0 :(得分:1)

如果名称包含空格,则必须将名称括在方括号中。

string sql ="
 INSERT INTO Empoyee_GI(Name,[Father Name], Birthdate, Address, City, Zone, Province, 
                       [Cell Number], Email, [Employement Status], Hire Date, 
                       [Renewal Date], Location, Position, BPS, Department, Gender, 
                       [Maritial Status], CNIC, [Employement Number])
 VALUES(...);";

SqlCeCommand cmd = new SqlCeCommand(sql,con);

请注意,您还应该使用React Router Documentation来避免sql注入!

相关问题