using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;namespace UpdataEmail
{
    public partial class Form1 : Form
    {
        string path = "";//路径        public Form1()
        {
            InitializeComponent();
        }        private void btcz_Click(object sender, EventArgs e)
        {
            OpenFileDialog dlg = new OpenFileDialog(); //选择文件对话框            dlg.DefaultExt = "TXT";
            dlg.Filter = "Text Files(*.TXT)|*.TXT||";            if (dlg.ShowDialog() == DialogResult.OK) //是否选中文件
            {
                this.textBox1.Text = dlg.FileName; //赋值给TextBox
            }
            path = this.textBox1.Text;  //获得路径 
        }        private void btgx_Click(object sender, EventArgs e)
        {
            if (path.Trim() == "")
            {
                MessageBox.Show("パスを選択してください!");
            }
            else
            {
                try
                {
                    //读取TXT文件
                    StreamReader sr = new StreamReader(path, System.Text.Encoding.Default);
                    StreamReader num = new StreamReader(path, System.Text.Encoding.Default);                    string linecount;
                    int count = 0;
                    while ((linecount = num.ReadLine()) != null)
                    {
                        count++;
                    }                    string[] name = new string[count];
                    string[] logno = new string[count];
                    string[] mobile = new string[count];
                    string[] email = new string[count];
                    string[] teltype = new string[count];                    string line;
                    int index = 0;                    while ((line = sr.ReadLine()) != null)
                    {
                        string[] strinfo = line.Split(',');
                        name[index] = strinfo[0].ToString();
                        logno[index] = strinfo[1].ToString();
                        mobile[index] = strinfo[2].ToString();
                        email[index] = strinfo[3].ToString();
                        teltype[index] = strinfo[4].ToString().Replace(';', ' ');                        index++;
                    }                    string strConnection = "user id=MAIL_USER;password=Doreal123;";
                    strConnection += "initial catalog=MAIL;Server=192.168.1.112;";
                    strConnection += "Connect Timeout=30";                    SqlConnection objConnection = new SqlConnection(strConnection);
                    objConnection.Open();
                    if (count == 0)
                    {
                        MessageBox.Show("選択のファイルは正しくありません! ");
                    }
                    else
                    {
                        for (int n = 0; n < count; n++)
                        {
                            SqlCommand comm = new SqlCommand();                            comm.CommandText = "UPDATE drsCustomer SET Eメールアドレス = '" + email[n] + "' WHERE 氏名漢字1='" + name[n] + "' AND 携帯電話番号 = '" + mobile[n] + "'";                            comm.Connection = objConnection;                            comm.CommandType = CommandType.Text;                            comm.ExecuteNonQuery();
                        }
                        objConnection.Close();                        MessageBox.Show("E-Mailアドレスは更新しました!");
                    }
                }
                catch
                {
                    MessageBox.Show("選択のファイルは正しくありません! ");
                }            }
        }        private void btzl_Click(object sender, EventArgs e)
        {
            this.Close();
        }
    }
}

解决方案 »

  1.   

    MFC写一个基于Dialog的程序。
    把btgx_Click中的代码,主要是读取数据,执行查询的操作改变为 C++下的即可。
      

  2.   

    void CDlgDemoDlg::OnBtnOpen() 
    {
    // TODO: Add your control notification handler code here
    CFileDialog fileDlg(1);
    fileDlg.m_ofn.lpstrDefExt = "TXT";
    fileDlg.m_ofn.lpstrFilter = "Text Files(*.TXT)\0*.TXT\0\0";
    if (IDOK == fileDlg.DoModal())
    {
    path = fileDlg.GetPathName();
    SetDlgItemText(IDC_EDIT_PATH,path);
    }
    }void CDlgDemoDlg::OnBtnGx() 
    {
    // TODO: Add your control notification handler code here
    int iStart = 0; //相当于String.Trim(),去掉所有空格
    while(-1 != (iStart = path.Find(" ",iStart)) )
    path.Delete(iStart);

    if (path == "")
    {
    MessageBox("パスを選択してください!");
        }
    else
    {
    TRY
    {
    //读取TXT文件
    CFile file(path,CFile::modeRead);
    CString sFile;
    file.Read((LPTSTR)(LPCTSTR)sFile,file.GetLength()); //下面读入数据处理的楼主自己应该会了吧

    }
    CATCH(CException,e)
    {
    MessageBox("選択のファイルは正しくありません! ");
    }
    END_CATCH
    }
    }代码测试过,可以运行的
      

  3.   


    void CDlgDemoDlg::OnBtnOpen() 
    {
    // TODO: Add your control notification handler code here
    CFileDialog fileDlg(1);
    fileDlg.m_ofn.lpstrDefExt = "TXT";
    fileDlg.m_ofn.lpstrFilter = "Text Files(*.TXT)\0*.TXT\0\0";
    if (IDOK == fileDlg.DoModal())
    {
    path = fileDlg.GetPathName();
    SetDlgItemText(IDC_EDIT_PATH,path);
    }
    }void CDlgDemoDlg::OnBtnGx() 
    {
    // TODO: Add your control notification handler code here
    int iStart = 0; //相当于String.Trim(),去掉所有空格
    while(-1 != (iStart = path.Find(" ",iStart)) )
    path.Delete(iStart);

    if (path == "")
    {
    MessageBox("パスを選択してください!");
        }
    else
    {
    TRY
    {
    //读取TXT文件
    CFile file(path,CFile::modeRead);
    CString sFile;
    file.Read((LPTSTR)(LPCTSTR)sFile,file.GetLength()); //下面读入数据处理的楼主自己应该会了吧

    }
    CATCH(CException,e)
    {
    MessageBox("選択のファイルは正しくありません! ");
    }
    END_CATCH
    }
    }