ExecuteNonQuery 溢出错误?

发布于 2024-12-17 19:30:02 字数 2889 浏览 2 评论 0原文

我一直在试图找出代码有什么问题。

我想做的是有两个用于性别的单选按钮,男性和男性。女性...

我希望当单击“男性”单选按钮时,将文本“男性”保存到性别字段中的数据库中(如果是文本类型),但我收到了溢出错误...

在添加单选按钮和 [性别] 字段,一切正常...

那么有什么帮助吗?

    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";
        }

    }
}

I've been trying to figure what is wrong with the code.

What I'm trying to do is that there are two Radiobuttons for gender, Male & Female...

I want that when Male radiobutton is clicked, the text MALE is saved into the database in the gender field which if of type text, but instead I'm getting an overflow error...

Before adding the radiobuttons and the [GENDER] field, everything was working fine...

So any help with it?

    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";
        }

    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

变身佩奇 2024-12-24 19:30:02

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

编辑...

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

You have an infinite loop. When the combo box changes the selectedindex event is raised, then you change the combo box and the event fires again.

Edited...

At the end of the button event handler you are changing the combo box Indexes again causing an endless loop of event firing and the ensuing stackoverflow

病毒体 2024-12-24 19:30:02

除了通过在连接的字符串中添加值来进行 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();

Aside from the possibility of SQL-Injection by adding your values in a concatenated string, you could also be failing if someone were to put in a name value like "O'Conner" where the quote in the name would terminate the string and throw the rest off.

Look into OleDbParameter object and setting them. Without exact syntax you would do something like

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文