using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;namespace sql
{
class Program
{
static void Main(string[] args)
{
SqlConnection myCon = new SqlConnection();
myCon.ConnectionString = @"Data Source=Localhost\ Sqlexpress ; Database=NorthWind;User Id=sa;Password=123;";
try
{
myCon.Open();
Console.WriteLine("{0}",myCon.State);
}
catch(Exception ex)
{
Console.WriteLine("未连接数据库!{0}",ex.Message);
}
finally
{
//关闭数据库
myCon.Close();
Console.WriteLine("{0}", myCon.State);
}
Console.ReadKey();
}
}
}
老是提示找不到服务器本机SQLServer的实例如何找到,如何设置。。
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;namespace sql
{
class Program
{
static void Main(string[] args)
{
SqlConnection myCon = new SqlConnection();
myCon.ConnectionString = @"Data Source=Localhost\ Sqlexpress ; Database=NorthWind;User Id=sa;Password=123;";
try
{
myCon.Open();
Console.WriteLine("{0}",myCon.State);
}
catch(Exception ex)
{
Console.WriteLine("未连接数据库!{0}",ex.Message);
}
finally
{
//关闭数据库
myCon.Close();
Console.WriteLine("{0}", myCon.State);
}
Console.ReadKey();
}
}
}
老是提示找不到服务器本机SQLServer的实例如何找到,如何设置。。
服务器名称就是打开Microsoft SQL Server Management Studio后,
你登录SQL时的服务器的名称
SqlConnection sql = new SqlConnection(sqlcon);
try
{
sql.Open();
MessageBox.Show("成功");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
sql.Close();
}
String con="Data Source=Localhost\ Sqlexpress ; Database=NorthWind;User Id=sa;Password=123;";
Sqlconnection testcon=new Sqlconnecttion(con);
try
{
sql.Open();
MessageBox.Show("成功");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
sql.Close();
}
Localhost\ Sqlexpress
connectionString="Data Source=.\sqlexpress;
Initial Catalog=BlogDB;uid=sa;pwd=1234;Persist Security Info=true"
providerName="System.Data.SqlClient" />把User Id=sa;Password=123改成uid pwd试试吧,我的是好用的!
同时你的sa和密码也得修改成和sql的一样。
刚做完毕业设计,很多同学买来之后不改sa和pwd。
都是连接不上的~我做的是web开发,还需要在webconfig里修改,不知道你的是什么。。
这里的Localhost为计算机名称 或者 可以写ip地址,
这里的Sqlexpress为SQL实例名 sqlserver安装的时候会让你填写实例名,
如果你的机子上只有一个sqlserver版本 而连接的数据库也是本机的话 完全可以用. local 来代替。