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

📄 form1.cs

📁 将数据库中的一列汉字批量转换为拼音第一个字母并存到另一列
💻 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;

namespace 中文转换
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            if (this.comboBox3.Text!= "" && this.comboBox4.Text!= "")
            {
                convert(this.comboBox3.SelectedItem.ToString(), this.comboBox4.SelectedItem.ToString());
            }
            else
            {
                MessageBox.Show("转换的列和目标列不能为空,请选择.");
                this.comboBox3.Focus();
            }
        }
        //转换
        public void convert(string col1,string col2)
        {

            string[] data=new string[500];
            this.listViewafter.Items.Clear();
            this.listViewafter.Columns.Clear();
       for (int i = 0; i <this.listviewfront.Columns.Count; i++)
            {
           this.listViewafter.Columns.Add(this.listviewfront.Columns[i].Text);
              
            }
            
               
                for (int j = 0; j < this.listviewfront.Items.Count; j++)
                {
                    for (int p = 0; p < this.listviewfront.Columns.Count; p++)
                {
                    data[p] = this.listviewfront.Items[j].SubItems[p].Text;
                
                    
                }
                this.listViewafter.Items.Add(new ListViewItem(data));
                }
                MessageBox.Show(col1,col2);
                
           //  int this.listViewafter.co
            this.listViewafter.Columns.RemoveByKey(col1);
          //  MessageBox.Show("c1="+c1.ToString()+"c2="+c2.ToString());
         //开始转换      
                for (int q = 0; q < this.listViewafter.Items.Count; q++)
                {
                    for (int k = 0; k < this.listViewafter.Columns.Count; k++)
                    {
                       
                     //string c2 = this.listViewafter.FindItemWithText(col2);
                   
                      //  if(this.listViewafter.Items[q].SubItems[k].
                       //this.listViewafter.Items[q].SubItems[k].Text=


                    }
                   
                }
                
            
        }
        //先定列
        private void selcol(string colname)
        {
            for (int i = 0; i < this.listViewafter.Items.Count; i++)
            {
                for (int j = 0; j < this.listViewafter.Columns.Count; j++)
                {
                    if (listViewafter.Columns[j].Text ==colname)
                    {
                        this.listViewafter.Select();
                    }
                    
                }

            }
        }
        //绑定表名
        private void bindtable()
        {
            this.comboBox2.Items.Clear();
            this.comboBox3.Items.Clear();
            this.comboBox4.Items.Clear();



            string database = this.textBox1.Text.Trim();
            string contring = "server=.;database=" + database + ";uid=sa;pwd=makea";
            SqlConnection sqlcon = new SqlConnection();
            sqlcon.ConnectionString = contring;
            SqlCommand opendata = new SqlCommand();
            opendata.Connection = sqlcon;
            string comtring = "select * from information_schema.tables where table_type='base table'";
            opendata.CommandText = comtring;
            try
            {
                sqlcon.Open();
            
            
            }
            catch { }
            
               
            
            
            try
            {
 SqlDataReader datasdr = opendata.ExecuteReader();
                while (datasdr.Read())
                {
                    if (datasdr["table_name"].ToString() != "dtproperties")
                    {

                        this.comboBox2.Items.Add(datasdr["table_name"].ToString());
                    }

                }
datasdr.Close();

            }
            catch (Exception X)
            {
               
            } 
            
            finally
            {
               
                sqlcon.Close();

            }


        }
        private void bindcom()
        {
            
            this.comboBox3.Items.Clear();
            this.comboBox4.Items.Clear();
            string database = this.textBox1.Text.Trim();
            string contring = "server=.;database=" + database + ";uid=sa;pwd=makea";
            SqlConnection sqlcon = new SqlConnection();
            sqlcon.ConnectionString = contring;
            SqlCommand opendata = new SqlCommand();
            opendata.Connection = sqlcon;
            string comtring = "select * from syscolumns where id =(select id from sysobjects where name='"+this.comboBox2.Text+"')";
            opendata.CommandText = comtring;
            try
            {
                sqlcon.Open();


            }
            catch (Exception x) { MessageBox.Show(x.ToString()); }

            try
            {
                SqlDataReader datasdr = opendata.ExecuteReader();
                while (datasdr.Read())
                {

                  
                        this.comboBox3.Items.Add(datasdr["name"].ToString());
                        this.comboBox4.Items.Add(datasdr["name"].ToString());

                }
                datasdr.Close();

            }
            catch (Exception X)
            {
                MessageBox.Show(X.ToString());
            }

            finally
            {

                sqlcon.Close();

            }


        }
       
        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            bindtable();
            this.comboBox2.Text = null;
            this.comboBox3.Text = null;
            this.comboBox4.Text = null;
            this.comboBox4.Items.Clear();
            this.comboBox3.Items.Clear();
           
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            bindcom();
            ydatabind();
        }

        private void comboBox3_SelectedIndexChanged(object sender, EventArgs e)
        {
           
           
         
         
        }

        private void ydatabind()
        {
            this.listviewfront.Columns.Clear();
            this.listviewfront.Items.Clear();
            this.comboBox3.Text = null;
            this.comboBox4.Text = null;

            string database = this.textBox1.Text.Trim();
            string contring = "server=.;database=" + database + ";uid=sa;pwd=makea";
            SqlConnection sqlcon = new SqlConnection();
            sqlcon.ConnectionString = contring;
            SqlCommand opendata = new SqlCommand();
            opendata.Connection = sqlcon;
            string comtring = "select * from syscolumns where id =(select id from sysobjects where name='" + this.comboBox2.Text + "')";
            opendata.CommandText = comtring;
            try
            {
                sqlcon.Open();
            }
            catch (Exception x) { MessageBox.Show(x.ToString()); }




            try
            {
                SqlDataReader datasdr = opendata.ExecuteReader();
             //绑定列
                while (datasdr.Read())
                {

                    this.listviewfront.Columns.Add(datasdr["name"].ToString());
                    
                  //  this.comboBox4.Items.Add(datasdr["name"].ToString());


                }
                datasdr.Close();
                //绑定数据
                SqlCommand datlistcom = new SqlCommand();
                datlistcom.CommandText = "select * from "+this.comboBox2.Text;
                datlistcom.Connection = sqlcon;
                SqlDataReader datalist=datlistcom.ExecuteReader();
                int i=datalist.FieldCount;
                string[] data=new string[i];
                
                 while(datalist.Read())
                {


                    for (int j = 0; j < i; j++)
                    {
                      data[j]=datalist.GetValue(j).ToString();
                    } listviewfront.Items.Add(new ListViewItem(data));
                        //this.listviewfront.Items.Add(new ListviewItem(new string[{datalist[1].ToString()}]));

                        //  this.comboBox4.Items.Add(datasdr["name"].ToString());

                    
                }
                datalist.Close();
            }
            catch (Exception X)
            {
                MessageBox.Show(X.ToString());
            }

            finally
            {

                sqlcon.Close();

            }
        }
        private void tdatabind()
        {

            this.listviewfront.Columns.Clear();
            this.listviewfront.Items.Clear();
            this.comboBox3.Text = null;


            string database = this.textBox1.Text.Trim();
            string contring = "server=.;database=" + database + ";uid=sa;pwd=makea";
            SqlConnection sqlcon = new SqlConnection();
            sqlcon.ConnectionString = contring;
            SqlCommand opendata = new SqlCommand();
            opendata.Connection = sqlcon;
            string comtring = "select * from syscolumns where id =(select id from sysobjects where name='" + this.comboBox2.Text + "')";
            opendata.CommandText = comtring;
            try
            {
                sqlcon.Open();
            }
            catch (Exception x) { MessageBox.Show(x.ToString()); }




            try
            {
                SqlDataReader datasdr = opendata.ExecuteReader();
                //绑定列
                while (datasdr.Read())
                {

                    this.listviewfront.Columns.Add(datasdr["name"].ToString());

                    //  this.comboBox4.Items.Add(datasdr["name"].ToString());


                }
                datasdr.Close();
                //绑定数据
                SqlCommand datlistcom = new SqlCommand();
                datlistcom.CommandText = "select * from " + this.comboBox2.Text;
                datlistcom.Connection = sqlcon;
                SqlDataReader datalist = datlistcom.ExecuteReader();
                int i = datalist.FieldCount;
                string[] data = new string[i];

                while (datalist.Read())
                {


                    for (int j = 0; j < i; j++)
                    {
                        data[j] = datalist.GetValue(j).ToString();
                    } listViewafter.Items.Add(new ListViewItem(data));
                    //this.listviewfront.Items.Add(new ListviewItem(new string[{datalist[1].ToString()}]));

                    //  this.comboBox4.Items.Add(datasdr["name"].ToString());


                }
                datalist.Close();
            }
            catch (Exception X)
            {
                MessageBox.Show(X.ToString());
            }

            finally
            {

                sqlcon.Close();

            }
        }

        private void tool_Click(object sender, EventArgs e)
        {
            second tool = new second();
            tool.Show();
        }
       

    }
    
}

⌨️ 快捷键说明

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