熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> .NET編程 >> 正文

c#中連接ODBC數據庫

2022-06-13   來源: .NET編程 

  

  using System;
        using SystemIO;
        using SystemDataSqlClient;
        namespace ConsoleSqlConnection
        {
            /// <summary>
            /// Class 的摘要說明
            /// </summary>
            class SqlConnectionClass
            {
                /// <summary>
                /// 應用程序的主入口點
                /// </summary>
                [STAThread]
                static void Main(string[] args)
                {
                    string strCon = @server=(local);Integrated Security=true;database=Northwind;uid=sa;pwd=sa;
                    SqlConnection MyCon = new SqlConnection(strCon);
                    // 打開SQL Server數據庫
                    try
                    {
                        MyConOpen();
                        // 浏覽數據庫
                        string strSQL = @select * from Customers;
                        SqlCommand MyCommand = new SqlCommand(strSQL MyCon);
                        // 將檢索結果放入SqlDataReader中
                        SqlDataReader MyDataReader = MyCommandExecuteReader();
                        ConsoleWriteLine(顯示數據庫中的數據);
                        while (MyDataReaderRead())
                        {
                            ConsoleWriteLine(用戶名稱:{} 公司名稱:{} 所在城市:{} MyDataReader[CustomerID]ToString()PadRight() MyDataReader[CompanyName]ToString()PadRight() MyDataReader[City]ToString());
                        }
                        MyDataReaderClose();
                    }
                    catch (Exception ex)
                    {
                        ConsoleWriteLine({} exToString());
                    }
                    finally
                    {
                        // 使用完畢關閉數據庫
                        MyConClose();
                    }
                    ConsoleReadLine();
                }
            }
        }


From:http://tw.wingwit.com/Article/program/net/201311/13640.html
    推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.