【问题标题】:how to insert Textfield value to database in C# for the following code如何在 C# 中将 Textfield 值插入数据库以获取以下代码
【发布时间】:2010-07-30 11:41:49
【问题描述】:
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;
using System.Data.Odbc;

namespace Username
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {

        }

        private void dateTimePicker1_ValueChanged(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "" && textBox2.Text == "" && textBox3.Text == "")
                MessageBox.Show("Plz Specify the UserName & Password","ERROR",MessageBoxButtons.OKCancel,MessageBoxIcon.Error);
            else if (textBox2.Text == "" && textBox3.Text == "")
                MessageBox.Show("Plz Specify the Password", "ERROR", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
            else if (textBox3.Text == "")
                MessageBox.Show("Plz RE Enter the Password", "ERROR", MessageBoxButtons.OKCancel, MessageBoxIcon.Error);
            else
            {
                string connectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Samples\\login.mdb";
               String uname, pass;
                uname = textBox1.Text;
                //System.Windows.Forms.MessageBox.Show(uname);
                pass = textBox2.Text; 
                OleDbConnection myConnection = new OleDbConnection(connectionString);
                myConnection.Open();
                int i=107;
                string query = "insert into EMPLOYEE_TABLE (ID,UserName,Password) VALUES (118,@uname,@pass)";

                //string query = "insert into LOGIN_TABLE (ID,UserName,Password) VALUES (i,'uname',' pass')";
                i++;

                OleDbCommand myCommand = new OleDbCommand();
                myCommand.CommandText = query;
                myCommand.Connection = myConnection;
                myCommand.ExecuteNonQuery();
                myConnection.Close();


                System.Windows.Forms.MessageBox.Show("User Account Succefully Created", "Caption", MessageBoxButtons.OKCancel, MessageBoxIcon.Information);

            }
        }
    }
}

【问题讨论】:

    标签: c# winforms oledb oledbcommand


    【解决方案1】:

    您快到了,但您需要填写您在查询中创建的参数:

    OleDbCommand myCommand = new OleDbCommand();
    myCommand.CommandText = query;
    
    //TODO: Review the datatypes and lengths for these parameters
    OleDbParameter myParm = myCommand.Parameters.Add("@uname", OleDbType.VarChar, 50);
    myParm.Value = textBox1.Text;
    
    myParm = myCommand.Parameters.Add("@pass", OleDbType.VarChar, 50);
    myParm.Value = textBox2.Text;
    
    myConnection.Open();
    myCommand.ExecuteNonQuery();
    myConnection.Close();
    

    有关命令参数的更多信息,请参阅this MSDN article

    【讨论】:

    • 您好,感谢您的回答,但是当我复制此代码时出现错误,因为 ExecuteNonQuery:Connection 属性尚未初始化那里的实际问题是什么?
    • 你是对的,示例不包括打开连接。现在确实如此。 :-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-24
    相关资源
    最近更新 更多