现在的位置: 首页 > 综合 > 正文

小结一下c#WinForm调用sql2000,将老表数据通过与新表数据比较,不同的添加进去,并使用进度条显示进度的教训与经验

2012年02月02日 ⁄ 综合 ⁄ 共 7996字 ⁄ 字号 评论关闭

这次的老数据库大概30万条记录,我第一次做,没太考虑效率,这样还是在朋友的帮助下实现的,完成大概需要2个小时(cpu:赛扬3.06,内存:1.5G,环境:VS2005+SQL2000),哈哈!

经验1:执行sql语句,不要笑。进来之前,我不知道如何执行多个sql语句,这也许是许多实习生的通病,要实现这个,可以建立多个sql语句,比如strSQL,strSQL1,strSQL2……strSQLi……,在多个SqlCommand对象中执行。

 

经验2:选择数据源,这也许会令许多初次实践者望而生畏,因为要访问多个数据库,可能不在一个服务器上,这也太难实现了吧!其实呢,这些也确实难以实现,可是我们的巨人前辈们已经为我们铺平路了,你所需要的只是跨出第一步,知道怎么告诉数据库去连接哪个数据库,无论其在哪台机器上。那么到底怎么做呢?

是这样的:定义字符串:

string strSQLSource = "Data Source=ip地址或者服务器名;Initial Catalog=你要访问的机器上的数据库名;User ID=用户名一般是sa;Password=密码";

Data Source=ip地址或者服务器名,表明你只能访问同一个域内的服务器,至于如何访问域外的服务器,我还不知道,呵呵,但肯定能。

后面的就不用解释了吧,呵呵……

当然,如果你想对多个服务器进行操作,也可以像经验1那样,定义多个string类型的strSQLSource1,strSQLSource2,……,strSQLSourcei,等等。接着用SqlConnection定义的对象执行。

 

贴一点源代码:

            string strSQLSource, strSQL

            strSQLSource = "Data Source=192.168.111.226;Initial Catalog=oldWXJS;User ID=sa;Password=";

            strSQL = "select * from tmk";
            SqlConnection sqlConn = new SqlConnection(strSQLSource);
            SqlCommand sqlComm = new SqlCommand(strSQL, sqlConn);
            sqlConn.Open();

 

经验3:当程序还未访问数据库是就出错了,你就按常规方法排错,什么是常规方法呢?就是机器帮你检查的类型,命名,运算符等错误。这些比较容易排查。连接数据库出错时,这部分错误,初学者多犯忘了打开数据库或关闭数据库的错误。这部分其实挺有规律的:常见的格式如下:

                    SqlConnection sqlConn2 = new SqlConnection(strSQLSource);
                    SqlCommand sqlComm2 = new SqlCommand(strSQL2, sqlConn2);
                    sqlConn2.Open();
                    int nFlag = Convert.ToInt32(sqlComm2.ExecuteScalar());
                    sqlConn2.Close();
  
          

另外还会常犯忘记转换sqlComm2.ExecuteScalar()类型的错误,如上面int nFlag = Convert.ToInt32sqlComm2.ExecuteScalar());写成int nFlag = sqlComm2.ExecuteScalar();

 

经验4:怎样利用断点获取信息。当调试没有错误后,你可能该迫不及待的点击执行数据库操作的按钮了,这时对于初学者来说肯定会出现比较奇怪的错误。而我这次见到的是:截取服务器: “消息 170,级别 15,状态 1,行 34
第 34 行: '新课标' 附近有语法错误。”“
将截断字符串或二进制数据。语句已终止。”等错误,这些错误主要是数据库方面的。比如说前者,是因为出现了特殊字符,那么我是怎么知道出现特殊字符了呢?就是利用断点,或者利用VS的错误提示,先看看变黄的那部分错误提示,然后看看下面临时变量框中的变量值,这个框通常和错误提示一起出现在主窗口的下部,下拉这个框的滚动条,你会看到各个变量出现错误是的值,一般对应sql语句的框条会在其右方出现下三角,你右击该三角,会出现三个可选项,选择一项你就可以查看当前的sql语句,如果你看不懂,就到sql查询分析器里好好琢磨,看看到底是哪里出错,结果也许你改变一下sql语句中的某个字符串中的字符,运行就通过了!这就是出现特殊字符了。后者的错误通常是由于你在数据库中定义了char型的字段,而老库中要加入该字段的数据长度超出了该字段的长度。那你就改成nvarchar或varcar或加长char的长度吧。

 

经验5:怎么写过滤函数?像这样:

        public string FilterString(string FilteringString)
        {
            string Str_FilteringString;

            //变量初始化
            Str_FilteringString = FilteringString;

            //过滤SQL的关键字符串
            Str_FilteringString = Str_FilteringString.Replace(";", ";");
            Str_FilteringString = Str_FilteringString.Replace("'", "‘");
            Str_FilteringString = Str_FilteringString.Replace("-", "");
            Str_FilteringString = Str_FilteringString.Replace(":", ":");

            return Str_FilteringString;
        }
Replace("'", "‘")表示用‘代替你从老库中读出来的字符串中的'。

怎么用呢?像这样:

                    strTflh = FilterString(DR["tflh"].ToString().Trim());
                    strTtm = FilterString(DR["ttm"].ToString().Trim());
                    strTzz = FilterString(DR["tzz"].ToString().Trim());

 

经验6:为进度条设置最大值!

像这样:

            strSQL1 = "select count(*) from tmk";
            SqlConnection sqlConnCount = new SqlConnection(strSQLSource);
            SqlCommand sqlCommCount = new SqlCommand(strSQL1,sqlConnCount);
            sqlConnCount.Open();
            int count =Convert.ToInt32(sqlCommCount.ExecuteScalar());
            progressUpdate.Maximum = count;

progressUpdate为进度条对象。

要用sqlCommCount.ExecuteScalar()呀,不要用sqlCommCount.ExecuteNonQuery(),因为:

ExecuteScalar:执行查询,并返回查询所返回的结果集中第一行的第一列。查询数据库表的行数,并返回忽略额外的列或行。

ExecuteNonQuery主要是用来执行更新和删除,并不返回任何值。实际上是对上述操作成功与否的标志返回。

 

先总结这些吧,头儿回来了!接着做多线程吧

贴一下完整的代码吧!大家可以共享!呵呵

 

 

private void btnUpdate_Click(object sender, EventArgs e)
        {
            string strTflh,strTtm,strTzz,strTwxly,strTcbs,strTjh,strTys,strTch,strTwxlx,strExchange,strTwz,strTUser,strTtm2,strTflh2,strTflh3,strTflh4,strTkeyword1,strTkeyword2,strTkeyword3,strTkeyword4;
            int strClicks, strRecord;
            DateTime DT_InDate = System.DateTime.Now;
            string strSQLSource, strSQL,strSQL1,strSQL2, strSQL3;
           
            strTflh=strTtm=strTzz=strTwxly=strTcbs=strTjh=strTys=strTch=strTwxlx=strExchange=strTwz=strTUser=strTtm2=strTflh2=strTflh3=strTflh4=strTkeyword1=strTkeyword2=strTkeyword3=strTkeyword4="";
            strClicks = strRecord = 0;

            strSQLSource = "Data Source=192.0.0.0;Initial Catalog=oldWXJS;User ID=sa;Password=";

            strSQL = "select * from tmk";
            SqlConnection sqlConn = new SqlConnection(strSQLSource);
            SqlCommand sqlComm = new SqlCommand(strSQL, sqlConn);
            sqlConn.Open();

            //返回老数据库数据的行数
            strSQL1 = "select count(*) from tmk";
            SqlConnection sqlConnCount = new SqlConnection(strSQLSource);
            SqlCommand sqlCommCount = new SqlCommand(strSQL1,sqlConnCount);
            sqlConnCount.Open();
            int count =Convert.ToInt32(sqlCommCount.ExecuteScalar());
            progressUpdate.Maximum = count;

            System.Data.SqlClient.SqlDataReader DR = sqlComm.ExecuteReader();

            progressUpdate.Visible = true;
           
            if (DR != null)
            {
                while (DR.Read())
                {
                    progressUpdate.Minimum = 0;
                    strRecord = Convert.ToInt32(DR.GetInt32(DR.GetOrdinal("record")));
                    strTflh = FilterString(DR["tflh"].ToString().Trim());
                    strTtm = FilterString(DR["ttm"].ToString().Trim());
                    strTzz = FilterString(DR["tzz"].ToString().Trim());

                    strTwxly = FilterString(DR["twxly"].ToString().Trim());
                    strTcbs = FilterString(DR["tcbs"].ToString().Trim());
                    strTjh = FilterString(DR["tjh"].ToString().Trim());
                    strTch = FilterString(DR["tch"].ToString().Trim());

                    strTys = FilterString(DR["tys"].ToString().Trim());
                    strTwxlx = FilterString(DR["twxlx"].ToString().Trim());
                    DT_InDate = DR.GetDateTime(DR.GetOrdinal("indate"));
                    strExchange = FilterString(DR["exchange"].ToString().Trim());
                    strTwz = FilterString(DR["twz"].ToString().Trim());

                    strSQL2 = "select count(*) as Number from test1 where record='" + strRecord + "' and tflh='" + strTflh + "' and ttm='" + strTtm + "' and tzz='" + strTzz + "' and twxly='" + strTwxly + "' and tcbs='" + strTcbs + "' and tjh='" + strTjh + "' and tch='" + strTch + "' and tys='" + strTys + "' and twxlx='" + strTwxlx + "' and indate='" + DT_InDate + "' and exchange='" + strExchange + "' and twz='" + strTwz + "'";
                   

                    SqlConnection sqlConn2 = new SqlConnection(strSQLSource);
                    SqlCommand sqlComm2 = new SqlCommand(strSQL2, sqlConn2);
                    sqlConn2.Open();
                    int nFlag = Convert.ToInt32(sqlComm2.ExecuteScalar());
                    sqlConn2.Close();
                                                        

                    if (nFlag < 0)
                    {
                        //查询过程出错,用MessageBox.Show()方法提示错误,

                        return;   //出错就跳出,不继续往下循环
                    }
                    else if (nFlag == 0)
                    {
                        strSQL3 = "insert into test1 values('" + strRecord + "','" + strTflh + "','" + strTtm + "','" + strTzz + "','" + strTwxly + "','" + strTcbs + "','" + strTjh + "','" + strTch + "','" + strTys + "','" + strTwxlx + "','" + DT_InDate + "','" + strExchange + "','" + strTwz + "','" + strTUser + "','" + strClicks + "','" + strTtm2 + "','" + strTflh2 + "','" + strTflh3 + "','" + strTflh4 + "','" + strTkeyword1 + "','" + strTkeyword2 + "','" + strTkeyword3 + "','" + strTkeyword4 + "')";
                        SqlConnection sqlConn3 = new SqlConnection(strSQLSource);
                        SqlCommand sqlComm3 = new SqlCommand(strSQL3, sqlConn3);
                        sqlConn3.Open();                       
                        int nFlag2 = sqlComm3.ExecuteNonQuery();
                        sqlConn3.Close();

                        if (nFlag2 != 1)
                        {
                            //提示插入出错,用MessageBox.Show()方法

                            return;   //出错就跳出,不继续往下循环
                        }
                        progressUpdate.Value += 1;
                    }
                }
                progressUpdate.Visible = false;
                MessageBox.Show(this,"更新完成!");
            }
            else
            {
                MessageBox.Show(this,"抱歉!您要添加的数据为空!");
            }
           
        }

       

抱歉!评论已关闭.