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

📄 form1.cs

📁 这是用C#和SQL相连的数据库程序
💻 CS
字号:
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.SqlClient;
//*******************************//
using System.Xml;

namespace ContactApp1128
{
    public partial class Form1 : Form
    {
        //全局变量,数据库联接
        public SqlConnection scon = null;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            try
            {
                //创建数据库联接
                string constr = "Data Source=D57457B0BCD7460;Initial Catalog=StudyDB;user id=sa;password='';";
                scon = new SqlConnection(constr);
                scon.Open();

                //读取数据库中所有记录
                SqlDataAdapter sda = new SqlDataAdapter("Select * from ContactTable", scon);
                DataSet ds = new DataSet();
                sda.Fill(ds, "contacttable");

                //显示数据
                if (ds.Tables[0].Rows.Count > 0)
                {
                    dataGridView1.DataSource = ds.Tables[0];
                }
                else
                {
                    MessageBox.Show("通信录中没有任何记录");
                }
             


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

        private void dataGridView1_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            textBox1.Text = dataGridView1.CurrentRow.Cells[0].Value.ToString();
            textBox2.Text = dataGridView1.CurrentRow.Cells[1].Value.ToString();
            comboBox1.Text = dataGridView1.CurrentRow.Cells[2].Value.ToString();
            textBox3.Text = dataGridView1.CurrentRow.Cells[3].Value.ToString();
            textBox4.Text = dataGridView1.CurrentRow.Cells[4].Value.ToString();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (scon == null)
            {
                MessageBox.Show("当前数据库连接无效");
                return;
            }

            try
            {

                string sql = "Insert into ContactTable(ContactName,ContactType,Phone,QQ,Address) values('"
                      + textBox1.Text + "','"
                      + comboBox1.Text + "','"
                      + textBox2.Text + "','"
                      + textBox3.Text + "','"
                      + textBox4.Text + "')";

                SqlCommand sqlcmd = new SqlCommand(sql);
                sqlcmd.Connection = scon;
                //执行插入操作
                sqlcmd.ExecuteNonQuery();

                UpdateView();

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

        }

        public void UpdateView()
        {
            try
            {
                //读取数据库中所有记录
                SqlDataAdapter sda = new SqlDataAdapter("Select * from ContactTable", scon);
                DataSet ds = new DataSet();
                sda.Fill(ds, "contacttable");

                //显示数据
                if (ds.Tables[0].Rows.Count > 0)
                {
                    dataGridView1.DataSource = ds.Tables[0];
                }
                else
                {
                    MessageBox.Show("通信录中没有任何记录");
                }



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

        private void button2_Click(object sender, EventArgs e)
        {
            if (scon == null)
            {
                MessageBox.Show("当前数据库连接无效");
                return;
            }

            try
            {

                string sql = "Update ContactTable set ContactType='"
                    +comboBox1.Text+"',Phone='"
                    +textBox2.Text+"',QQ='"
                    +textBox3.Text+"',Address='"
                    +textBox4.Text+"'where ContactName='"
                    +textBox1.Text+"'";

                SqlCommand sqlcmd = new SqlCommand(sql);
                sqlcmd.Connection = scon;
                sqlcmd.ExecuteNonQuery();

                UpdateView();

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

        private void button3_Click(object sender, EventArgs e)
        {
            if (scon == null)
            {
                MessageBox.Show("当前数据库连接无效");
                return;
            }

            try
            {

                string sql = "Delete ContactTable where ContactName='"
                    + textBox1.Text + "'";

                SqlCommand sqlcmd = new SqlCommand(sql);
                sqlcmd.Connection = scon;
                sqlcmd.ExecuteNonQuery();

                UpdateView();

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

        private void button4_Click(object sender, EventArgs e)
        {
            if (scon == null)
            {
                MessageBox.Show("当前数据库连接无效");
                return;
            }

            try
            {

                string sql = "select ContactName from ContactTable where  ContactName='"
                      + textBox1.Text + "'";
                


                SqlCommand sqlcmd = new SqlCommand(sql);
                sqlcmd.Connection = scon;


                if (sql!=null)
                {
                    int aa = dataGridView1.RowCount ;
                   for(int i=0;i<aa;i++)
                   {

                     if (dataGridView1.Rows[i].Cells [0].Value.ToString().Trim ()==textBox1.Text.Trim ())
                        {
                            comboBox1.Text = dataGridView1.Rows[i].Cells[2].Value.ToString();
                            textBox2.Text = dataGridView1.Rows[i].Cells[1].Value.ToString();
                            textBox3.Text = dataGridView1.Rows[i].Cells[3].Value.ToString();
                            textBox4.Text = dataGridView1.Rows[i].Cells[4].Value.ToString();
                        }
                    }

                }
              
                sqlcmd.ExecuteNonQuery();
                UpdateView();

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

        private void button5_Click(object sender, EventArgs e)
        {
            SqlDataAdapter sda = new SqlDataAdapter("select * from ContactTable", scon);
            DataSet ds = new DataSet();
            sda.Fill(ds, "contacttable");
            OpenFileDialog sfd = new OpenFileDialog();
            if (sfd.ShowDialog() == DialogResult.OK)
            {
                XmlDataDocument doc = new XmlDataDocument();
                doc.Load(sfd.FileName);
                XmlNode root = doc.DocumentElement;


                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    XmlElement newcontactname = doc.CreateElement("contactname");
                    XmlElement newcontacttype = doc.CreateElement("newcontacttype");
                    XmlElement newphone = doc.CreateElement("phone");
                    XmlElement newQQ = doc.CreateElement("QQ");
                    XmlElement newaddress = doc.CreateElement("address");
                    XmlElement newcontact = doc.CreateElement("contact");


                    newcontactname.InnerText = ds.Tables[0].Rows[i][0].ToString().Trim();
                    newcontacttype.InnerText = ds.Tables[0].Rows[i][1].ToString().Trim();
                    newphone.InnerText = ds.Tables[0].Rows[i][2].ToString().Trim();
                    newaddress.InnerText = ds.Tables[0].Rows[i][3].ToString().Trim();
                    newQQ.InnerText = ds.Tables[0].Rows[i][4].ToString().Trim();



                    newcontact.AppendChild(newcontactname);
                    newcontact.AppendChild(newcontacttype);
                    newcontact.AppendChild(newphone);
                    newcontact.AppendChild(newaddress);
                    newcontact.AppendChild(newQQ);
                    root.AppendChild(newcontact);
                }

                doc.Save(sfd.FileName);

            }
        }
    }
}

⌨️ 快捷键说明

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