欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

openfiledialog读取txt写入数据库示例

程序员文章站 2023-12-20 11:50:58
winform 中添加 openfiledialog button, winform .cs 中添加本地.mdf,如下: 复制代码 代码如下:using system;u...

winform 中添加 openfiledialog button, winform .cs 中添加本地.mdf,如下:

复制代码 代码如下:

using system;
using system.collections.generic;
using system.linq;
using system.windows.forms;

namespace txt记事本文件的读写
{
    static class program
    {
        /// <summary>
        /// 应用程序的主入口点。
        /// </summary>
        [stathread]
        static void main()
        {
            //sqlserver 附加mdf文件
            string datadir = appdomain.currentdomain.basedirectory;
            if (datadir.endswith(@"\bin\debug\") || datadir.endswith(@"\bin\release\"))
            {
                datadir = system.io.directory.getparent(datadir).parent.parent.fullname;
                appdomain.currentdomain.setdata("datadirectory", datadir);
            }

            application.enablevisualstyles();
            application.setcompatibletextrenderingdefault(false);
            application.run(new form1());
        }
    }
}

读取txt中的数据写入db:

复制代码 代码如下:

using system;
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;
using system.io;

namespace txt记事本文件的读写
{
    public partial class form1 : form
    {
        public form1()
        {
            initializecomponent();
        }

        private void btnreadtxt_click(object sender, eventargs e)
        {

            if (odfimport.showdialog() == dialogresult.ok)
            {
                using (sqlconnection conn = new sqlconnection(@"data source=.\sqlexpress;attachdbfilename=|datadirectory|\telphoneno.mdf;integrated security=true;user instance=true"))
                {
                    conn.open();
                    using (filestream filestream = file.openread(odfimport.filename))  //打开txt文件
                    {
                        using (streamreader stmreader = new streamreader(filestream))  //读取txt文件
                        {
                            string line = null;
                            string telno = "";
                            string name = "";
                            string strins = "";

                            //sql 参数
                            strins = "insert into phoneno(telno,name) values(@telno,@name) ";
                            sqlparameter[] sqlpara = new sqlparameter[] {
                                    new sqlparameter("telno",telno),
                                    new sqlparameter("name",name)
                                };
                            //把读取出来的数据写入.mdf
                            using (sqlcommand sqlcmd = new sqlcommand(strins, conn))
                            {
                                //逐行读取
                                while ((line = stmreader.readline()) != null)
                                {
                                    string[] strtel = line.split('-');
                                    telno = strtel[0].tostring();
                                    name = strtel[1].tostring();

                                    sqlcmd.parameters.addrange(sqlpara);
                                    sqlcmd.executenonquery();
                                    sqlcmd.parameters.clear(); //参数清除
                                }
                                messagebox.show("导入成功", "read txt");
                            }
                        }
                    }
                }
            }
            else
            {
                return;
            }

        }
    }
}

上一篇:

下一篇: