Chào mừng đến với Diễn đàn lập trình - Cộng đồng lập trình.
Kết quả 1 đến 5 của 5

Chủ đề: kết nối với CSDL

  1. #1

  2. #2
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    Mã:
    using System;using System.Data;using System.Data.SqlClient; public class KetNoiSQLServer{    public static void Main()    {        // Tạo connection string để kết nối        string connectionString = @"Server = .\SQLEXPRESS;" +                                   "Integrated Security = true";        // Tạo một connection tới máy chủ        SqlConnection conn = new SqlConnection(connectionString);                // QUA TRINH KET NOI        try        {            // Mở kết nối            conn.Open();            Console.WriteLine("Connection opened !");        }        catch (SqlException sqle)        {            // Thông báo biến cố khi kết nối            Console.WriteLine("Error: " + sqle.Message);        }        finally        {            // Đóng kết nối            conn.Close();            Console.WriteLine("Connection closed !");        }    }}
    Nguyên văn lấy từ đây nè : http://forums.congdongcviet.com/showthread.php?t=3130

    ==================================================
    Chú ý: Cho code và tag code (nên sử dụng tag code nâng cao) - Sunflower

  3. #3
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    Câu ở trên chỉ là kết nối sử dụng chứng thực của windows thôi.


    Mã:
    connectionstring = "Integrated Security = false; server=(local); database=; uid= ; pwd= ;";
    Thêm thông tin vào câu kết nối. Có thể thay local = tên máy sử dụng

  4. #4
    Ngày tham gia
    Sep 2015
    Bài viết
    0
    đâu là kết nối trong winform nè:

    Mã:
    using System;using System.Data.Odbc; namespace QUANLYBANHANG.DATA{    /// <summary>    /// Summary description for DBConnection.    /// </summary>    public class DBConnect    {               private string strConnectionString;        private OdbcConnection connection = null;        public OdbcConnection   Connection        {            get{return connection;}        }         private string DatabaseFile;        private string DatabasePath;        private string strLoiPhatSinh = "";        private OdbcCommand     myCommand   = null;        private OdbcDataReader  dataReader  = null;         public string LoiPhatSinh        {            get {return strLoiPhatSinh;}        }         public DBConnect()        {            //            // TODO: Add constructor logic here            //        }         public void SetDatabaseFile(string databaseFile)        {            DatabaseFile = databaseFile;        }         public void SetDatabasePath(string databasePath)        {            DatabasePath = databasePath;        }         public bool Open()        {            string strConn = "Provider=SQLOLEDB;Driver=SQL Server;Server=(local);Database=TRUONGTRUNGHOC;user id=sa;Pwd=sa";             try            {                connection = new OdbcConnection();                connection.ConnectionString = strConnectionString;                connection.Open();            }            catch(Exception ex)            {                strLoiPhatSinh = ex.Message;                return false;            }            return true;        }         public bool OpenFileTach()        {            string strConn = "Provider=SQLOLEDB;Driver=SQL Server;Server=(local);Database=TRUONGTRUNGHOC;user id=sa;Pwd=sa";             try            {                connection = new OdbcConnection();                connection.ConnectionString = strConnectionString;                connection.Open();            }            catch(Exception ex)            {                strLoiPhatSinh = ex.Message;                return false;            }            return true;        }         public void Close()        {            if(connection != null)            {                connection.Close();            }        }         public OdbcDataReader OpenReader(string strSqlStatement)        {            try             {                myCommand = new OdbcCommand(strSqlStatement, connection);                dataReader = myCommand.ExecuteReader();            }            catch(Exception ex)            {                strLoiPhatSinh = ex.Message;                return (OdbcDataReader)null;            }             return dataReader;        }         public void DeleteMyCommand()        {            myCommand.Dispose();        }         public OdbcCommand CreateCommand(string strSelectQuery)        {            return new OdbcCommand(strSelectQuery, connection);        }    }}
    -------------------------------------------------------------------------------
    Chú ý: Cho code vào tag code, nên sử dụng tag code nâng cao - Sunflower

  5. #5
    Mình thường dùng câu kết nối như vầy nè:

    Mã:
    string source = "server=.;database=database;uid=tên đăng nhập;pwd=password";SqlConnection con = new SqlConnection(source);con.Open();{}con.Close();

 

 

Quyền viết bài

  • Bạn Không thể gửi Chủ đề mới
  • Bạn Không thể Gửi trả lời
  • Bạn Không thể Gửi file đính kèm
  • Bạn Không thể Sửa bài viết của mình
  •