⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 form1.cs

📁 是个好用的!!!你愉快 的来用的哦.那可以是很好的哟立刻内好家伙棵开幕厂力量女爸爸 方法法环境22饿的大公国各个是是常常方法
💻 CS
字号:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace mySQLDataAdapter
{
    public partial class Form1 : Form
    {
        sqlDataAdapter sqlDataAdapter1;
        sqlCommand sqlCommand1;
        sqlConnection sqlConnection1;
        private bool bNewRecord = false;
        public Form1()
        {
            InitializeComponent();
        }
        private void GetCustomID()
        {
            sqlDataReader sdr;
            sqlConnection1.Open();
            sdr = sqlCommand1.ExecuteReader(CommandBehavior.CloseConnection);
            cbxID.Items.Clear();
            while (sdr.Read())
            {
                cbxID.Items.Add(sdr.GetValue(0));
                
            }
            sdr.Close();
            cbxID.SelectedIndex = 0;
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            String sConnString = "Data Source=(local)Initial Catalog=Northwind;Integrated Security=True";
            string sSQL = "SELECT * FROM Customers";
            sqlConnection1 = new
                System.Data.SqlClient.SqlConnection(sConnString);
            sqlCommand1 = new
                System.Data.SqlClient.SqlCommand(sSQL,sqlConnection1);
            sqlCommand1.CommandText = "SELECT CustomerID FROM Customers ORDER BY CustomerID";
            sqlDataAdapter1 = new sqlDataAdapter(sSQL,sqlConnection1);
            DataSet dataSet1 = new DataSet();
            sqlDataAdapter1.Fill(dataSet1,"Customers");
            dataGridView1.DataSource=dataSet1.Tables["Customers"];
            GetCustomID();
        }

        private void cbxID_SelectedIndexChanged(object sender, EventArgs e)
        {
            sqlCommand sqlcmd = new sqlCommand("SELECT * FROM Customers WHERE CustomerID=@ID",sqlConnection1);
            sqlcmd.Parameters.AddWithValue("@ID",cbxID.Text);
            sqlDataReader sdr;
            sqlConnection1.Open();
            sdr = sqlcmd.ExecuteReader();
            if (sdr.Read())
            {
                txtCompanyName.Text = sdr.GetString(1);
                txtContactName.Text = sdr["ContactName"].ToString();
                txtContactTitle.Text = sdr[3].ToString();
                txtAddress.Text = sdr.GetValue(4).ToString();
                txtCity.Text = sdr["City"].ToString();
                txtRegion.Text = sdr.GetValue(6).ToString();
                txtPostalCode.Text = sdr[7].ToString();
                txtCountry.Text = sdr[8].ToString();
                txtPhone.Text = sdr[9].ToString();
                txtFax.Text = sdr[10].ToString();
            }
            sdr.Close();
            sqlConnection1.Close();
        }

        private void btnPrev_Click(object sender, EventArgs e)
        {
            if (cbxID.SelectedIndex > 0)
                cbxID.SelectedIndex -= 1;
        }

        private void btnNext_Click(object sender, EventArgs e)
        {
            if (cbxID.SelectedIndex <cbxID.Items.Count-1)
                cbxID.SelectedIndex += 1;
        }

        private void btnOrder_Click(object sender, EventArgs e)
        {
            FormOrders orders = new FormOrders();
            orders.CustomerID = cbxID.Text;
            orders.ShowDialog();
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            txtCompanyName.Text = "";
            txtContactName.Text = "";
            txtContactTitle.Text = "";
            txtAddress.Text = "";
            txtCity.Text = "";
            txtRegion.Text = "";
            txtPostalCode.Text = "";
            txtCountry.Text = "";
            txtPhone.Text = "";
            txtFax.Text = "";
            cbxID.DropDownStyle = ComboBoxStyle.DropDown;
            cbxID.Text = "";
            bNewRecord = true;
        }

        private void btnDelete_Click(object sender, EventArgs e)
        {
            sqlCommand sqlcmd = new sqlCommand("DELETE FROM Customers WHERE CustomerID=@ ID",sqlConnection1);
            sqlcmd.Parameters.AddWithValue("@ ID",cbxID.Text);
            try
            {
                sqlConnection1.Open();
                int rowAffected = sqlcmd.ExecuteNonQuery();
                if (rowAffected == 1)
                    cbxID.Items.Remove(cbxID.SelectedIndex);
            }
            catch (sqlException ex)
            {
                MessageBox.Show("删除错误:" + ex.Message, "出现错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            finally
            {
                sqlConnection1.Close();
            }
            if (cbxID.SelectedIndex < cbxID.Items.Count - 1)
                cbxID.SelectedIndex += 1;
                
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            string sqlStatement;
            if (bNewRecord == true)
            {
                sqlStatement = "INSERT INTO Customers VALUES("+
                    "'"+cbxID.Text+"',"+
                    "'"+txtCompanyName.Text + "',"+
                    "'"txtContactName.Text + "',"+
                    "'"txtContactTitle.Text + "',"+
                    "'"txtAddress.Text + "',"+
                    "'"txtCity.Text + "',"+
                    "'"txtRegion.Text + "',"+
                    "'"txtPostalCode.Text + "',"+
                    "'"txtCountry.Text + "',"+
                    "'"txtPhone.Text + "',"+
                    "'"txtFax.Text + "')";

            }
            else
            {
                sqlStatement="UPDATE Customers SET"+
                    "CompanyName='"+txtCompanyName.Text+"',"+
                    "ContactName='"+txtContactName.Text+"',"+
                    "ContactTitle='"+txtContactTitle.Text+"',"+
                    "Address='"+txtAddress.Text+"',"+
                    "City='"+txtCity.Text+"',"+
                    "Region='"+txtRegion.Text+"',"+
                    "PostalCode='"+txtPostalCode.Text+"',"+
                    "Country='"+txtCountry.Text+"',"+
                    "Phone='"+txtPhone.Text+"',"+
                    "Fax='"+txtFax.Text+"',"+
                    "WHERE CustomerID='"+cbxID.Text+"'";
            }
        }
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -