首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

C# 初始化数据库的有关问题

2012-12-19 
C#初始化数据库的问题C#初始化数据库的问题数据库是sqlserver2000有一个data.sql文件里面有中文资料,但我

C# 初始化数据库的问题
C#  初始化数据库的问题

数据库是sqlserver2000

有一个data.sql文件

里面有中文资料,但我写入到数据中全是乱码

我写入的方法:
    public  int ExecuteSqlScript(string sqlFile)
        {
            int returnValue = -1;
            int sqlCount = 0, errorCount = 0;
            if (!File.Exists(sqlFile))
            {
                //Log.WriteLog(string.Format("sql file not exists!", sqlFile));
                return -1;
            }
            using (StreamReader sr = new StreamReader(sqlFile,Encoding.Unicode))
            {
                string line = string.Empty;
                char spaceChar = ' ';
                string newLIne = "\r\n", semicolon = ";";
                string sprit = "/", whiffletree = "-";
                string sql = string.Empty;
                do
                {
                    line = sr.ReadLine();
                    // 文件结束
                    if (line == null) break;
                    // 跳过注释行
                    if (line.StartsWith(sprit) || line.StartsWith(whiffletree)) continue;
                    // 去除右边空格
                    line = line.TrimEnd(spaceChar);
                    sql += line;
                    // 以分号(;)结尾,则执行SQL
                    if (sql.EndsWith(semicolon))
                    {
                        try


                        {
                            sqlCount++;
                            //SqlHelper.ExecuteNonQuery(sql, null);
                           InsertData(sql);
                            
                        }
                        catch (Exception ex)
                        {
                            errorCount++;
                            //Log.WriteLog(sql + newLIne + ex.Message);
                        }
                        sql = string.Empty;
                    }
                    else
                    {
                        // 添加换行符
                        if (sql.Length > 0) sql += newLIne;
                    }
                } while (true);
            }
            if (sqlCount > 0 && errorCount == 0)
                returnValue = 1;
            if (sqlCount == 0 && errorCount == 0)
                returnValue = 0;
            else if (sqlCount > errorCount && errorCount > 0)
                returnValue = -1;


            else if (sqlCount == errorCount)
                returnValue = -2;
            return returnValue;
        }

改怎么觉解?谢谢!
[最优解释]
Encoding.Unicode,编码问题。
[其他解释]

引用:
Encoding.Unicode,编码问题。


已解决 自己的问题 谢谢!

热点排行