未知的语法错误

时间:2019-10-16 01:42:50

标签: c#

我正在尝试创建一个应用程序,以将信息从Windows窗体读取到数据库中。当我在代码视图中时,它说没有错误,但是,当我尝试将数据读入数据库时​​,它说:“'s'附近的语法不正确,并且字符串')'后面的引号引起来”。猜它在查询中的某个地方,但我不知道在哪里。有帮助吗?

private void button1_Click(object sender, EventArgs e)
        {
            int age;
            int income;
            int zipcode;
            string desiredServices;
            string maritalStatus;
            string occupation;
            string education;
            int children;
            string livingStatus;
            string veteran;
            string race;
            int custYears;

            age = int.Parse(this.age.Text);
            income = int.Parse(this.income.Text);
            zipcode = int.Parse(zip.Text);
            desiredServices = services.Text;
            maritalStatus = marital.Text;
            occupation = this.occupation.Text;
            education = this.education.Text;
            children = int.Parse(this.children.Text);
            livingStatus = living.Text;
            veteran = this.veteran.Text;
            race = this.race.Text;
            custYears = int.Parse(years.Text);

            string constring = "Data Source (LocalDB)\\MSSQLLocalDB;AttachDbFilename=C:\\Users\\aggiefanrob\\source\\repos\\AgileProject_rlk73\\AgileProject_rlk73\\Agile.mdf;Integrated Security=True";
            string query = ("insert into Patient (Patient_Age, Patient_Income, Patient_Zip, Desired_Service, Marital_Status, Patient_Job, Education_Level, Patient_Kids, Living_Status, Veteran_Status,Patient_Race, Years_As_Customer) Values ('" + this.age.Text + "','" + this.income.Text + "','" + zip.Text + "','" + services.Text + "','" + marital.Text + "','" + this.occupation.Text + "','" + this.education.Text + "','" + this.children.Text + "','" + living.Text + "','" + this.veteran.Text + "','" + this.race.Text + "','" + years.Text + "')");
            SqlConnection conDatabase = new SqlConnection(constring);
            SqlCommand cmdDataBase = new SqlCommand(query, conDatabase);
            SqlDataReader myReader;

            try
            {
                conDatabase.Open();


                myReader = cmdDataBase.ExecuteReader();
                cmdDataBase.ExecuteNonQuery();
                MessageBox.Show("Data has been entered");
                while (myReader.Read())
                {

                }
            }catch(Exception ex)
            {
            MessageBox.Show(ex.Message);
            }

0 个答案:

没有答案