DbDataAdapter.Update方法

本文介绍了一种使用C#和EasyFuncLib.dll库来批量更新数据库中特定字段的方法,该方法能够将一个中文名称字段转换为其拼音首字母缩写形式。
 


现在又一个数据库,有goodname字段,全是中文名称,现在需要增加一个字段shorname,是中文名称每个汉字的首字母。转换过程调用EasyFuncLib.dll

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
//网上可以找到这个DLL
//主要用途是讲汉语转换为拼音如“中华人民共和国”转化为"zhrmghg"
using EasyFuncLib;
using System.Data.SqlClient;
using System.Data;
using System.Data.OleDb;

namespace translate
{
    class Program
    {
        /// <summary>
        /// 实现修改数据库shortname列,将其值变为goodname的拼音首字母缩写。
         /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            //连接字符串,必须输入Provider=SQLOLEDB
            string connectionString = "Data Source=.;Initial Catalog=kuweichaxun;User ID=sa;pwd=sa;Provider=SQLOLEDB";
            using (OleDbConnection conn = new OleDbConnection(connectionString))
            {
                //查询字符串,必须包含主键,否则不会自动生成更新语句
                   string SQLString = "select [row],[floor],[line],[bit],[goodname],[shortname] from [storeinfo]";
                DataSet ds = new DataSet();
                try
                {
                    conn.Open();
                    OleDbDataAdapter adapter = new OleDbDataAdapter();
                    adapter.SelectCommand = new OleDbCommand(SQLString, conn);
                    OleDbCommandBuilder builder = new OleDbCommandBuilder(adapter);
                    adapter.Fill(ds);
                    foreach (DataTable table in ds.Tables)
                    {
                        foreach (DataRow dr in table.Rows)
                        {
                            string older = dr[4].ToString();
                            //将goodname转化为shortname
                            dr[5] = Chinese2PinYin.GetFirstPinYin(older);
                            Console.WriteLine(dr[5]);
                        }
                    }
                    //更新到数据库
                    adapter.Update(ds);
                }
                catch (System.Data.SqlClient.SqlException E)
                {
                    conn.Close();
                    throw new Exception(E.Message);
                }
            }

        }
    }
}


************** 异常文本 ************** code = Error (1), message = System.Data.SQLite.SQLiteException (0x800007BF): SQL logic error no such table: TempAscii 在 System.Data.SQLite.SQLite3.Prepare(SQLiteConnection cnn, String strSql, SQLiteStatement previous, UInt32 timeoutMS, String& strRemain) 在 System.Data.SQLite.SQLiteCommand.BuildNextCommand() 在 System.Data.SQLite.SQLiteCommand.GetStatement(Int32 index) 在 System.Data.SQLite.SQLiteDataReader.NextResult() 在 System.Data.SQLite.SQLiteDataReader..ctor(SQLiteCommand cmd, CommandBehavior behave) 在 System.Data.SQLite.SQLiteCommand.ExecuteReader(CommandBehavior behavior) 在 System.Data.SQLite.SQLiteCommand.ExecuteDbDataReader(CommandBehavior behavior) 在 System.Data.Common.DbCommand.System.Data.IDbCommand.ExecuteReader(CommandBehavior behavior) 在 System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior) 在 System.Data.Common.DbDataAdapter.Fill(DataTable[] dataTables, Int32 startRecord, Int32 maxRecords, IDbCommand command, CommandBehavior behavior) 在 System.Data.Common.DbDataAdapter.Fill(DataTable dataTable) 在 TempPatrol.SystemDBDataSet_AsciiTableAdapters.TempAsciiTableAdapter.Fill(TempAsciiDataTable dataTable) 在 TempPatrol.MainForm.MainForm_Load(Object sender, EventArgs e) 在 System.EventHandler.Invoke(Object sender, EventArgs e) 在 System.Windows.Forms.Form.OnLoad(EventArgs e) 在 DevExpress.XtraEditors.XtraForm.OnLoad(EventArgs e)
06-27
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值