我的SQL Server连接字符串设置如下:
String strConnection = @"Data Source=servername;Initial Catalog=dbname; User ID =user; Password =pass;";
然后我有一个组合框,它显示该数据库中的所有表。连接字符串可以在一个数据库(初始目录)中正常工作。但是,如果我想从同一服务器上的2个数据库中提取表,该怎么办? SQL Server中的用户可以访问两个数据库。那我用什么连接字符串呢?
最简单的方法是Initial Catalog=dbname,db2name
。但这当然行不通。
答案 0 :(得分:0)
如果该用户确实确实具有访问其他数据库的权限-您可以仅在ChangeDatabase
上使用SqlConnection
方法-像这样:
string strConnection = @"Data Source=servername;Initial Catalog=dbname; User ID =user; Password =pass;";
using (SqlConnection conn = new SqlConnection(strConnection))
{
// do what you want to do with "dbname"
......
// switch to "db2name" - on the same server, with the same credentials
conn.ChangeDatabase("db2name");
// do what you want to do with "db2name"
......
}
答案 1 :(得分:0)
我最终在表单上使用了两个组合框。一种用于数据库,另一种用于表。当我在第一个组合框中选择一个数据库时,第二个组合框会自动显示该数据库中的表格。对我来说,使用不同的连接使用两个组合框要容易得多。 这是我的解决方案代码的一部分:
public partial class Form1 : Form
{
SqlDataAdapter sda;
SqlCommandBuilder scb;
DataTable dt;
SqlDataAdapter sda2;
SqlCommandBuilder scb2;
DataTable dt2;
public Form1()
{
InitializeComponent();
}
//ON FORM LOAD
private void Form1_Load(object sender, EventArgs e)
{
String stringConnection = @"Data Source=SERVER_NAME; Initial Catalog =DB_NAME; User ID =USER; Password =PASS;";
SqlConnection con2 = new SqlConnection(stringConnection);
try
{
con2.Open();
SqlCommand sqlCmd2 = new SqlCommand();
sqlCmd2.Connection = con2;
sqlCmd2.CommandType = CommandType.Text;
sqlCmd2.CommandText = "SELECT name FROM sys.databases EXCEPT SELECT name FROM sys.databases WHERE name='master' OR name='model' OR name='msdb' OR name='tempdb'";
SqlDataAdapter sqlDataAdap2 = new SqlDataAdapter(sqlCmd2);
DataTable dtRecord2 = new DataTable();
sqlDataAdap2.Fill(dtRecord2);
dtRecord2.DefaultView.Sort = "name ASC";
comboBox2.DataSource = dtRecord2;
comboBox2.DisplayMember = "NAME";
comboBox2.DisplayMember = "NAME";
con2.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
//BUTTON FOR SHOWING TABELS IN DATAGRIDVIEW
private void ShowTbl_Click(object sender, EventArgs e)
{
string selected = this.ComboBox1.GetItemText(this.ComboBox1.SelectedItem);
string DBselected = this.comboBox2.GetItemText(this.comboBox2.SelectedItem);
SqlConnection con = new SqlConnection(@"Data Source=SERVER_NAME;Initial Catalog =" + DBselected + "; User ID=USER;Password=PASS;");
sda = new SqlDataAdapter(@"SELECT * FROM dbo.[" + selected + "]", con);
dt = new DataTable();
sda.Fill(dt);
dataGridView1.DataSource = dt;
string ComboBoxSelected = ComboBox1.GetItemText(ComboBox1.SelectedItem);
con.Close();
}
//WHEN I SELECT DATABASE IN COMBOBOX2, COMBOBOX1 DISPLAYS TABLES IN THAT DATABASE
private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
{
string selectedbase = this.comboBox2.GetItemText(this.comboBox2.SelectedItem);
string aa = comboBox2.SelectedText;
String strConnection = @"Data Source=SERVER_NAME;Initial Catalog =" + selectedbase+ "; User ID =USER; Password =PASS;";
SqlConnection con = new SqlConnection(strConnection);
try
{
con.Open();
SqlCommand sqlCmd = new SqlCommand();
sqlCmd.Connection = con;
sqlCmd.CommandType = CommandType.Text;
sqlCmd.CommandText = "Select table_name from information_schema.tables";
SqlDataAdapter sqlDataAdap = new SqlDataAdapter(sqlCmd);
DataTable dtRecord = new DataTable();
sqlDataAdap.Fill(dtRecord);
dtRecord.DefaultView.Sort = "table_name ASC";
ComboBox1.DataSource = dtRecord;
ComboBox1.DisplayMember = "TABLE_NAME";
ComboBox1.DisplayMember = "TABLE_NAME";
con.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
答案 2 :(得分:-1)
编辑开始
1)如果要显示所有数据库中的所有表,请以相同的方式填充组合框。 参考:How do I list all tables in all databases in SQL Server in a single result set?
2)或者,您可以在表组合框之前提供另一个组合框以显示数据库列表,一旦用户选择了数据库名称,就将其传递给查询。这是一个有用的选择,因为在两个数据库中都有一个具有相同名称的表的可能性。 (例如:db_1和db_2中的t_users)
3)如果您限于两个数据库,请使用上述(1)中的where子句
编辑结束
如果您要访问一台服务器和一个实例的多个数据库,只要该用户和一个数据库可以访问到一个服务器和一个实例中的一个数据库,就可以建立一个连接。如果所有数据库都在同一服务器和实例中,则可以查询任何数据库,请参见下面的示例。
SELECT a.ID, b.ID
FROM Database1.dbo.table1 a
INNER JOIN Database2.dbo.table2 b on a.ID = b.ID
对于您而言,您可以对存储过程的查询类似于以下查询。
select *
from sys.tables --to fetch list of tables from the DB which you app connected to
select *
from IAMS_Discr_Complaints.sys.tables --to fetch tables from another DB on the same server