c#将指定数据库中所有数据由简体转换为繁体

/*
a、注意数据库编码要能兼容gb2312和big5,比如MySql中使用utf8
b、该代码采用遍历的方式,并用MySqlCommandBuilder进行批量更新,所以能转换的表必须包含主键,不包括主键的表则不能转换
c、引用了Microsoft.VisualBasic.dll进行简繁转换
*/
using  System;
using  System.Data;
using  MySql.Data;
using  MySql.Data.MySqlClient;
using  System.Collections.Generic;
using  System.Text;
using  Microsoft.VisualBasic;

namespace  Gb2312ToBig5
{
    
class  Program
    {
        
static   void  Main( string [] args)
        {
            
// 入口
            Console.WriteLine( " 请输入数据库所在IP: " );
            
string  ip  =  Console.ReadLine().Trim();

            Console.WriteLine(
" 请输入数据库名称: " );
            
string  db  =  Console.ReadLine().Trim();

            Console.WriteLine(
" 请输入登录数据库用户名: " );
            
string  user  =  Console.ReadLine().Trim();

            Console.WriteLine(
" 请输入登录数据库密码: " );
            
string  psw  =  Console.ReadLine();

            
string  connectionString  =   " Data Source= "   +  ip  +   " ;User ID= "   +  user  +   " ;Password= "   +  psw  +   " ;DataBase= "   +  db  +   " ;Allow Zero Datetime=true;Charset=utf8; " ;

            Console.WriteLine(
" 生成的数据库连接字符串为:{0},继续吗?(Y/N) " , connectionString);
            
if  (Console.ReadLine().ToString().ToUpper()  ==   " Y " )
            {
                
// 包含所有表名称的DataTable
                DataTable dtAll  =  tableList(connectionString);
                
if  (dtAll  !=   null )
                {
                    
if  (dtAll.Rows.Count  >   0 )
                    {
                        Console.Write(
" 转换中,请稍候: " );
                        
for  ( int  i  =   0 ; i  <  dtAll.Rows.Count; i ++ )
                        {
                            dtConvert(dtAll.Rows[i][
0 ].ToString(), connectionString);
                        }
                    }
                }
            }
        }

        
// 将DataTable中每行每列转为繁体
         private   static   void  dtConvert( string  dtName,  string  connectionString)
        {
            
string  sql  =   "" ;
            MySqlCommand cmd 
=   null ;
            MySqlDataAdapter da 
=   null ;
            DataTable dt 
=   null ;
            MySqlCommandBuilder builder 
=   null ;

            
using  (MySqlConnection conn  =   new  MySqlConnection(connectionString))
            {
                
try
                {
                    sql 
=   " select * from  "   +  dtName;
                    cmd 
=   new  MySqlCommand(sql, conn);
                    conn.Open();
                    da 
=   new  MySqlDataAdapter(cmd);
                    
// 添加主键映射
                    da.MissingSchemaAction  =  MissingSchemaAction.AddWithKey;
                    dt 
=   new  DataTable();
                    da.Fill(dt);

                    
// 遍历dt做替换
                     if  (dt.Rows.Count  >   0 )
                    {
                        
// 如果表包含主键
                         if  (dt.PrimaryKey.Length  >   0 )
                        {
                            
#region  遍历
                            
for  ( int  i  =   0 ; i  <  dt.Rows.Count; i ++ )
                            {
                                
for  ( int  j  =   0 ; j  <  dt.Columns.Count; j ++ )
                                {
                                    
if  (dt.Columns[j].DataType.ToString()  ==   " System.String " )
                                    {
                                        
if  (dt.Rows[i][j]  !=   null )
                                        {
                                            
if  (dt.Rows[i][j].ToString()  !=   string .Empty)
                                            {
                                                dt.Rows[i][j] 
=  getBig5(dt.Rows[i][j].ToString());
                                                Console.Write(
" . " );
                                            }
                                        }
                                    }
                                }
                            }
                            
#endregion

                            builder 
=   new  MySqlCommandBuilder(da);
                            da.Update(dt);
                        }
                    }
                    
// 释放资源
                    builder.Dispose();
                    cmd.Dispose();
                    da.Dispose();
                    dt.Clear();
                    dt.Dispose();
                    
                }
                
catch  (Exception error)
                {
                    Console.WriteLine(error.ToString());
                }
                
finally
                {
                    conn.Close();
                }
                
            }
        }

        
// 遍历每个表
         private   static  DataTable tableList( string  connectionString)
        {
            DataTable dt 
=   new  DataTable();

            
using  (MySqlConnection conn  =   new  MySqlConnection(connectionString))
            {
                
// SHOW TABLES为MySQL列出所有表,如SQLServer请使用相关命令
                MySqlCommand cmd  =   new  MySqlCommand( " SHOW TABLES " ,conn);
                MySqlDataAdapter da 
=   new  MySqlDataAdapter(cmd);
                DataSet ds 
=   new  DataSet();

                
try
                {
                    conn.Open();
                    da.Fill(ds, 
" temp_tables " );
                    dt 
=  ds.Tables[ " temp_tables " ];
                }
                
catch  (Exception error)
                {
                    Console.WriteLine(error.ToString());
                }
                
finally
                {
                    conn.Close();
                }
            }

            
return  dt;
        }

        
// 简体转繁体
         private   static   string  getBig5( string  gb2312)
        {
            
string  big5  =   "" ;
            
if  ((gb2312  !=   null &&  (gb2312  !=  String.Empty))
            {
                gb2312 
=  gb2312.Trim();
                big5 
=  Strings.StrConv(gb2312,VbStrConv.TraditionalChinese, 0 );
            }
            
return  big5;
        }

    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用JavaScript将数据库数据显示为折线图,你需要完成以下步骤: 1. 从数据库获取需要显示的数据。 2. 将数据转换为适合折线图显示的格式,例如JSON格式。 3. 在HTML添加一个canvas元素,用于绘制折线图。 4. 在JavaScript使用第三方图表库(例如Chart.js)来绘制折线图,并将数据传递给该库。 以下是一个简单的示例代码,演示如何使用Chart.js将数据库数据显示为折线图: ```csharp // 从数据库获取需要显示的数据 string connectionString = "Data Source=myServerAddress;Initial Catalog=myDataBase;User ID=myUsername;Password=myPassword;"; string query = "SELECT * FROM myTable"; SqlConnection connection = new SqlConnection(connectionString); SqlDataAdapter adapter = new SqlDataAdapter(query, connection); DataTable dataTable = new DataTable(); adapter.Fill(dataTable); // 将数据转换为JSON格式 List<object> dataPoints = new List<object>(); foreach (DataRow row in dataTable.Rows) { object[] dataPoint = new object[2]; dataPoint[0] = row["xValue"]; dataPoint[1] = row["yValue"]; dataPoints.Add(dataPoint); } string jsonData = JsonConvert.SerializeObject(dataPoints); // 在HTML添加canvas元素 <canvas id="myChart"></canvas> // 在JavaScript使用Chart.js绘制折线图 var ctx = document.getElementById('myChart').getContext('2d'); var chartData = JSON.parse('@Html.Raw(jsonData)'); var chartOptions = { type: 'line', data: { datasets: [{ label: 'My Dataset', data: chartData, fill: false, borderColor: 'rgb(75, 192, 192)', tension: 0.1 }] }, options: { responsive: true, plugins: { legend: { position: 'top', }, title: { display: true, text: 'My Chart' } }, scales: { x: { display: true, title: { display: true, text: 'X Axis' } }, y: { display: true, title: { display: true, text: 'Y Axis' } } } } }; var myChart = new Chart(ctx, chartOptions); ``` 上述代码,我们使用了JsonConvert将数据转换为JSON格式,并使用Chart.js绘制折线图。你需要根据自己的数据结构和需求进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值