[英]C# SQL Server connection string with multiple databases on same server
我有這樣設置的SQL Server連接字符串:
String strConnection = @"Data Source=servername;Initial Catalog=dbname; User ID =user; Password =pass;";
然后,我有一個組合框,顯示該數據庫中的所有表。 連接字符串可以在一個數據庫(初始目錄)中正常工作。 但是,如果我想從同一服務器上的2個數據庫中提取表,該怎么辦? SQL Server中的用戶可以訪問兩個數據庫。 那我用什么連接字符串呢?
最簡單的方法是Initial Catalog=dbname,db2name
。 但這當然行不通。
如果該用戶確實具有訪問其他數據庫的權限-您可以僅在SqlConnection
上使用ChangeDatabase
方法-就像這樣:
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"
......
}
我最終在表單上使用了兩個組合框。 一種用於數據庫,另一種用於表。 當我在第一個組合框中選擇一個數據庫時,第二個組合框會自動顯示該數據庫中的表格。 對我來說,使用不同的連接使用兩個組合框要容易得多。 這是我的解決方案代碼的一部分:
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);
}
}
}
編輯開始
1)如果要顯示所有數據庫中的所有表,請在組合框中填充相同的內容。 參考: 如何在單個結果集中列出SQL Server所有數據庫中的所有表?
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.