问题一:怎么判断连接SqlServer数据库成功,然后返回提示信息???
问题二:知道数据库用户名和密码,怎么返回SqlServer中所有数据库的名称???
29 个解决方案
#1
SqlConnection conn = new SqlConnection("Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;");
conn.Open();
conn.Open();
#2
报错就连接失败了
#3
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
using (SqlConnection connection = new SqlConnection(""))
{
connection.Open();
if (connection.State == ConnectionState.Open)
{
//已经打开
}
}
}
}
#4
1、判断是否链接成功,只用try{}catch(){}来捕获异常就可以了
2、SqlServer中所有数据库的名称,只有winform可以做到,web没有这样的权限,不过好像也有偏激的做法,直接去读取system.table去查找
2、SqlServer中所有数据库的名称,只有winform可以做到,web没有这样的权限,不过好像也有偏激的做法,直接去读取system.table去查找
#5
USE master
SELECT dbid, DB_NAME(dbid) AS DB_NAME
FROM sysdatabases
ORDER BY dbid
GO
SELECT dbid, DB_NAME(dbid) AS DB_NAME
FROM sysdatabases
ORDER BY dbid
GO
#6
或者你写个查询语句啊,还可以连接服务器测试一下啊
Tools-connect to database
Tools-connect to database
#7
try
{
SqlConnection conn = new SqlConnection("Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;");
conn.Open();
}
catch
{
//连接失败
}
#8
SqlConnection conn = new SqlConnection("Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;");
try
{
conn.Open();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
finally
{
conn.Close();
}
连接失败会异常,捕捉并处理
连接master数据库,查询SysDatabases表,就能查询所有数据库
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=master;User Id=sa;Password=111;");
try
{
conn.Open();
SqlCommand cmd = new SqlCommand("SELECT Name FROM SysDatabases ORDER BY Name", conn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
finally
{
conn.Close();
}
GridView1.DataSource = dt;
GridView1.DataBind();
}
#9
通过系统库master去查找所有的库
select name from sysdatabases
select name from sysdatabases
#10
不抱错就成功了啊
做个if判断一下也行啊
做个if判断一下也行啊
#11
谢谢,试试看 。。。
#12
1、if (connection.State == ConnectionState.Open)
2、select name from sysdatabases
2、select name from sysdatabases
#13
学习了
#14
sqlconneticon的 open方法
#15
正解
#16
就是用在“WInform”里的
#17
谢谢大家。
#18
try
{
链接正确
}
catch (Exception ex)
{
链接错误
}
{
链接正确
}
catch (Exception ex)
{
链接错误
}
#19
你只要执行
select @@version之类的语句就行了,这就证明sql server系统连接成功
#20
楼主,试试这个,我一直这样用:
private SqlConnection connection;
public SqlConnection Connection
{
get
{
string connectionString = "server=.;database=DB;uid=SA;pwd=123456";
if (connection == null)
{
connection = new SqlConnection(connectionString);
connection.Open();//这里就是打开了,说明连接上了
}
else if (connection.State == System.Data.ConnectionState.Closed)
{
connection.Open(); //这里就是打开了,说明连接上了
}
else if (connection.State == System.Data.ConnectionState.Broken)
{
connection.Close();
connection.Open();//这里就是打开了,说明连接上了
}
return connection; //不管什么情况,这里总是返回一个打开的连接
}
}
private SqlConnection connection;
public SqlConnection Connection
{
get
{
string connectionString = "server=.;database=DB;uid=SA;pwd=123456";
if (connection == null)
{
connection = new SqlConnection(connectionString);
connection.Open();//这里就是打开了,说明连接上了
}
else if (connection.State == System.Data.ConnectionState.Closed)
{
connection.Open(); //这里就是打开了,说明连接上了
}
else if (connection.State == System.Data.ConnectionState.Broken)
{
connection.Close();
connection.Open();//这里就是打开了,说明连接上了
}
return connection; //不管什么情况,这里总是返回一个打开的连接
}
}
#21
web.config
<add name="ConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True"
providerName="System.Data.SqlClient" />
在.CS里面
SqlConnection con = new SqlConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
try{
con.open();
response.write("<script>alert('数据库连接陈功')</script>");
try{
string sql="select name from sysdatabases";
SqlDataAdapter da = new SqlDataAdapter(sql, con);
DataTable dt = new DataTable();
da.fill(dt);
return dt;
}
catch{
response.write("<script>alert('数据库读取失败')</script>");
}
}
catcah{
response.write("<script>alert('数据库连接失败')</script>");
}
<add name="ConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True"
providerName="System.Data.SqlClient" />
在.CS里面
SqlConnection con = new SqlConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
try{
con.open();
response.write("<script>alert('数据库连接陈功')</script>");
try{
string sql="select name from sysdatabases";
SqlDataAdapter da = new SqlDataAdapter(sql, con);
DataTable dt = new DataTable();
da.fill(dt);
return dt;
}
catch{
response.write("<script>alert('数据库读取失败')</script>");
}
}
catcah{
response.write("<script>alert('数据库连接失败')</script>");
}
#22
SqlConnection conn=new SqlConnection("Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword");
con.open();
response.write("连接成功!");
con.close();
con.open();
response.write("连接成功!");
con.close();
#23
connection.State就是连接状态判断啊。
if(connection.State == System.Data.ConnectionState.Open)
{
MessageBox.show("Success!");
}
if(connection.State == System.Data.ConnectionState.Open)
{
MessageBox.show("Success!");
}
#25
vs的Tools-connect to database 试试,如果成功就可以了
#26
Try
Catch
语句组合。
.state属性。
Catch
语句组合。
.state属性。
#27
+1
#28
不用考虑。1101编程 仅中文版 正版免费快速开发
#29
还是要思考明白的
#1
SqlConnection conn = new SqlConnection("Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;");
conn.Open();
conn.Open();
#2
报错就连接失败了
#3
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
using (SqlConnection connection = new SqlConnection(""))
{
connection.Open();
if (connection.State == ConnectionState.Open)
{
//已经打开
}
}
}
}
#4
1、判断是否链接成功,只用try{}catch(){}来捕获异常就可以了
2、SqlServer中所有数据库的名称,只有winform可以做到,web没有这样的权限,不过好像也有偏激的做法,直接去读取system.table去查找
2、SqlServer中所有数据库的名称,只有winform可以做到,web没有这样的权限,不过好像也有偏激的做法,直接去读取system.table去查找
#5
USE master
SELECT dbid, DB_NAME(dbid) AS DB_NAME
FROM sysdatabases
ORDER BY dbid
GO
SELECT dbid, DB_NAME(dbid) AS DB_NAME
FROM sysdatabases
ORDER BY dbid
GO
#6
或者你写个查询语句啊,还可以连接服务器测试一下啊
Tools-connect to database
Tools-connect to database
#7
try
{
SqlConnection conn = new SqlConnection("Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;");
conn.Open();
}
catch
{
//连接失败
}
#8
SqlConnection conn = new SqlConnection("Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword;");
try
{
conn.Open();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
finally
{
conn.Close();
}
连接失败会异常,捕捉并处理
连接master数据库,查询SysDatabases表,就能查询所有数据库
protected void Page_Load(object sender, EventArgs e)
{
DataTable dt = new DataTable();
SqlConnection conn = new SqlConnection("Data Source=.;Initial Catalog=master;User Id=sa;Password=111;");
try
{
conn.Open();
SqlCommand cmd = new SqlCommand("SELECT Name FROM SysDatabases ORDER BY Name", conn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
finally
{
conn.Close();
}
GridView1.DataSource = dt;
GridView1.DataBind();
}
#9
通过系统库master去查找所有的库
select name from sysdatabases
select name from sysdatabases
#10
不抱错就成功了啊
做个if判断一下也行啊
做个if判断一下也行啊
#11
谢谢,试试看 。。。
#12
1、if (connection.State == ConnectionState.Open)
2、select name from sysdatabases
2、select name from sysdatabases
#13
学习了
#14
sqlconneticon的 open方法
#15
正解
#16
就是用在“WInform”里的
#17
谢谢大家。
#18
try
{
链接正确
}
catch (Exception ex)
{
链接错误
}
{
链接正确
}
catch (Exception ex)
{
链接错误
}
#19
你只要执行
select @@version之类的语句就行了,这就证明sql server系统连接成功
#20
楼主,试试这个,我一直这样用:
private SqlConnection connection;
public SqlConnection Connection
{
get
{
string connectionString = "server=.;database=DB;uid=SA;pwd=123456";
if (connection == null)
{
connection = new SqlConnection(connectionString);
connection.Open();//这里就是打开了,说明连接上了
}
else if (connection.State == System.Data.ConnectionState.Closed)
{
connection.Open(); //这里就是打开了,说明连接上了
}
else if (connection.State == System.Data.ConnectionState.Broken)
{
connection.Close();
connection.Open();//这里就是打开了,说明连接上了
}
return connection; //不管什么情况,这里总是返回一个打开的连接
}
}
private SqlConnection connection;
public SqlConnection Connection
{
get
{
string connectionString = "server=.;database=DB;uid=SA;pwd=123456";
if (connection == null)
{
connection = new SqlConnection(connectionString);
connection.Open();//这里就是打开了,说明连接上了
}
else if (connection.State == System.Data.ConnectionState.Closed)
{
connection.Open(); //这里就是打开了,说明连接上了
}
else if (connection.State == System.Data.ConnectionState.Broken)
{
connection.Close();
connection.Open();//这里就是打开了,说明连接上了
}
return connection; //不管什么情况,这里总是返回一个打开的连接
}
}
#21
web.config
<add name="ConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True"
providerName="System.Data.SqlClient" />
在.CS里面
SqlConnection con = new SqlConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
try{
con.open();
response.write("<script>alert('数据库连接陈功')</script>");
try{
string sql="select name from sysdatabases";
SqlDataAdapter da = new SqlDataAdapter(sql, con);
DataTable dt = new DataTable();
da.fill(dt);
return dt;
}
catch{
response.write("<script>alert('数据库读取失败')</script>");
}
}
catcah{
response.write("<script>alert('数据库连接失败')</script>");
}
<add name="ConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True"
providerName="System.Data.SqlClient" />
在.CS里面
SqlConnection con = new SqlConnection();
con.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
try{
con.open();
response.write("<script>alert('数据库连接陈功')</script>");
try{
string sql="select name from sysdatabases";
SqlDataAdapter da = new SqlDataAdapter(sql, con);
DataTable dt = new DataTable();
da.fill(dt);
return dt;
}
catch{
response.write("<script>alert('数据库读取失败')</script>");
}
}
catcah{
response.write("<script>alert('数据库连接失败')</script>");
}
#22
SqlConnection conn=new SqlConnection("Data Source=myServerAddress;Initial Catalog=myDataBase;User Id=myUsername;Password=myPassword");
con.open();
response.write("连接成功!");
con.close();
con.open();
response.write("连接成功!");
con.close();
#23
connection.State就是连接状态判断啊。
if(connection.State == System.Data.ConnectionState.Open)
{
MessageBox.show("Success!");
}
if(connection.State == System.Data.ConnectionState.Open)
{
MessageBox.show("Success!");
}
#24
#25
vs的Tools-connect to database 试试,如果成功就可以了
#26
Try
Catch
语句组合。
.state属性。
Catch
语句组合。
.state属性。
#27
+1
#28
不用考虑。1101编程 仅中文版 正版免费快速开发
#29
还是要思考明白的