DbDataAdapter.Update方法

來源:互聯網
上載者:User
 

現在又一個資料庫,有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);                }            }        }    }}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.