现在只是在本地上练习,还没有服务器来练习远程连接,以后再补。
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(); } }}