現在只是在本地上練習,還沒有服務器來練習遠程連接,以後再補。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
namespace sqlserver
{
class Program
{
static void Main(string[] args)
{
SqlConnection conn = new SqlConnection("Server=服務器名;user id=用戶名;password=密碼;initial catalog=表;Connect Timeout=30");
//根據個人習慣,合起來寫或分開寫
//string str = "Server=服務器名;user id=用戶名;password=密碼;initial catalog=表;Connect Timeout=30";
//SqlConnection conn = new SqlConnection(str);
try
{
conn.Open();
Console.WriteLine("數據庫連接成功");
}
catch (System.Exception ex)
{
Console.WriteLine("數據庫連接失敗");
}
Console.ReadKey();
}
}
}