c#连接数据库MS SQL Server

c#连接数据库MS SQL Server
方法一:
可以利用SqlConnectionStringBuilder,这样不必去记住名称。
using System.Data.Sql;
using System.Data.SqlClient;
    SqlConnectionStringBuilder scsb = new SqlConnectionStringBuilder();
    scsb.DataSource = @"(local)\SQLExpress";
    //scsb.DataSource = @"192.168.2.200";//如果连接专用SQL服务器,则输入IP地址
    scsb.IntegratedSecurity = false;
    scsb.InitialCatalog = "ERP"; //数据库名 
    scsb.UserID = "sa";
    scsb.Password = "Aaz1397";
    SqlConnection myConnection = new SqlConnection(scsb.ConnectionString);

方法二:
using System.Data.Sql;
using System.Data.SqlClient;
DataTable dt = new DataTable();
string con = "Server = (local)\\SQLEXPRESS;database = ERP;user = sa;pwd = Aaz1397;Initial Catalog=pcmis;";//本地数据库服务器
string str1 = string.Empty;
str1 = string.Format("SELECT [UserId] as 帐号 ,[Processing] as 制程 FROM [dbo].[Authority_MU]  where  1=2");
SqlConnection conn = new SqlConnection(con);
SqlDataAdapter adapter = new SqlDataAdapter();
conn.Open();
adapter.SelectCommand = new SqlCommand(str1, conn);

string con = "user=sa;pwd=Aaz1397;database=ERP;server=(local)\\SQLEXPRESS";(连接SQL Express,非Server版,并且使用双反斜线\\)
string con = "user=sa;pwd=Aaz1397;database=ERP;server=.";(连接本地SQL Server版,用.即可)
string con = "user=sa;pwd=Aaz1397;database=ERP;server=192.168.2.200";(连接公司专用SQL Server服务器,用IP地址)

已标记关键词 清除标记
相关推荐
©️2020 CSDN 皮肤主题: 大白 设计师:CSDN官方博客 返回首页