ExecuteNonQuery溢出错误?

时间:2011-11-25 18:46:22

标签: overflow oledb

我一直试图弄清楚代码有什么问题。

我要做的是有两个性别的Radiobutton,男性和女性。女...

我希望单击Male radiobutton时,文本MALE会保存到性别字段的数据库中,如果是text类型的话,我会收到溢出错误...

在添加radiobuttons和[GENDER]字段之前,一切正常......

所以对它有任何帮助吗?

    using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.OleDb;

namespace OfflineRF
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        string gender;

        private void button1_Click(object sender, EventArgs e)
        {
            string ORF1 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = C:\\OfflineRF.mdb";
            OleDbConnection conn = new OleDbConnection(ORF1);
            conn.Open();
            OleDbCommand cmd = new OleDbCommand();
            cmd.Connection = conn;
            cmd.CommandText = "INSERT INTO OFFRF([Fname], [Lname], [NIC], [Gender], [HomeTel], [Cellphone], [Passengers], [From], [To])VALUES('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + textBox7.Text + textBox8.Text +"','"+gender+"','" + textBox4.Text + "','" + textBox5.Text + "','" + textBox6.Text + "','" + comboBox1.Text + "','" + comboBox2.Text + "')";
            cmd.ExecuteNonQuery();
            conn.Close();
            System.Windows.Forms.MessageBox.Show("Form Saved Successfully !", "Caption", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);

            textBox1.Text = "";
            textBox2.Text = "";
            textBox3.Text = "";
            textBox4.Text = "";
            textBox5.Text = "";
            textBox6.Text = "";
            textBox7.Text = "";
            textBox8.Text = "";
            comboBox1.SelectedIndex = -1;
            comboBox2.SelectedIndex = -1;
        }

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

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox1.Text == "Karachi")
            {
                comboBox2.Items.Clear();
                comboBox2.Items.Add("Sukkur");
                comboBox2.Items.Add("Hyderabad");
            }
            else if (comboBox1.Text == "Sukkur")
            {
                comboBox2.Items.Clear();
                comboBox2.Items.Add("Karachi");
                comboBox2.Items.Add("Hyderabad");
            }
            else
            {
                comboBox2.Items.Clear();
                comboBox2.Items.Add("Karachi");
                comboBox2.Items.Add("Sukkur");
            }
        }

        private void Male_CheckedChanged(object sender, EventArgs e)
        {
            if (Male.Checked)
                gender = "Male";
            else
                gender = "Female";
        }

    }
}

2 个答案:

答案 0 :(得分:1)

你有一个无限循环。当组合框更改时,会引发selectedindex事件,然后更改组合框并再次触发事件。

...编辑

在按钮事件处理程序的末尾,您再次更改组合框索引,导致无限循环的事件触发和随后的stackoverflow

答案 1 :(得分:1)

除了通过在连接字符串中添加值来实现SQL注入的可能性之外,如果有人要输入名称值,如“O'Conner”,名称中的引号将终止字符串,您也可能会失败把剩下的休息一下。

查看OleDbParameter对象并设置它们。没有确切的语法,你会做类似

的事情
string ORF1 = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source = C:\\OfflineRF.mdb";
OleDbConnection conn = new OleDbConnection(ORF1);
conn.Open();
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = conn;
cmd.CommandText = "INSERT INTO OFFRF( Fname, Lname, NIC, Gender, HomeTel, "
           + "Cellphone, Passengers, [From], [To] ) VALUES "
           + "(  ?, ?, ?, ?, ?, ?, ?, ?, ? )";
// Add parameters in same ordinal position as the "?" place-holders
// the first parameter is more of generic description of WHAT it is for and 
// does NOT have to exactly match the column name, the second parameter is
// the actual value that should be put into the database.  This same context
// is used for performing other SQL actions (select, delete, update, etc)
// to help prevent SQL injection.
cmd.Parameters.Add( "valForFName", textBox1.Text );
cmd.Parameters.Add( "valForLName", textBox2.Text );
cmd.Parameters.Add( "valForNIC", textBox3.Text + textBox7.Text + textBox8.Text );
// Not sure of syntax here, but get proper text from your radio choice of gender into string
gender = YourForm.RadioForGender.SelectedItem.Text;  
cmd.Parameters.Add( "valForGender", gender );
cmd.Parameters.Add( "valHomePhone", textBox4.Text );
cmd.Parameters.Add( "valCell", textBox5.Text );
cmd.Parameters.Add( "howmany", textBox6.Text );
cmd.Parameters.Add( "forFromValue", comboBox1.Text );
cmd.Parameters.Add( "forToValue",  comboBox2.Text );
cmd.ExecuteNonQuery();
conn.Close();