How do I connect to a SQL database from C#?

后端 未结 8 1357
-上瘾入骨i
-上瘾入骨i 2020-12-02 19:06

I am trying to write a local program management and install system for my home network, and I think I\'ve got the technologies nailed down:

  • C#/.NET/WPF for the
8条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2020-12-02 19:50

    SqlConnection

    object is made for this.

    Eg:

    SqlConnection conn = new SqlConnection(
        "Data Source=(local);Initial Catalog=Northwind;Integrated Security=SSPI"); 
    

    or

    SqlConnection conn = new SqlConnection(
    "Data Source=DatabaseServer; Initial Catalog=Northwind; User ID=YourUserID; Password=YourPassword");
    
    conn.Open(); // opens the database connection
    

    Edit:

    After doing all your stuff you have to close the connection by

    conn.Close();
    

    Data Source: Identifies the server. Could be local machine, machine domain name, or IP Address.

    Initial Catalog: Database name.

    Integrated Security: Set to SSPI to make connection with user's Windows login

    User ID: Name of user configured in SQL Server.

    Password: Password matching SQL Server User ID.

提交回复
热议问题