1
我可以在列表框中顯示選定表名的「腳本表作爲創建查詢」的結果。如何在文本框中顯示「腳本表作爲創建查詢」的結果以及數據類型的長度?
的SelectedIndexChanged列表框的:
QueryOfSelectedTableName.Text = CREATE TABLE TableName (Id int NOT NULL, Name nvarchar NOT NULL, SurName nvarchar NOT NULL, Adress nvarchar NOT NULL)
但我需要表的數據類型的lenght這樣的:
QueryOfSelectedTableName.Text= CREATE TABLE TableName (Id int NOT NULL, Name nvarchar(50) NOT NULL, SurName nvarchar(50) NOT NULL, Adress nvarchar(50) NOT NULL)
我如何能做到這一點的代碼
private void ListBoxTableNames_SelectedIndexChanged(object sender, EventArgs e)
{
string table = ListBoxTableNames.SelectedItem.ToString();
using (SqlConnection con = new SqlConnection(strConnectDB1))
{
con.Open();
using (SqlCommand comQuery = new SqlCommand(@"declare @vsSQL varchar(8000)
declare @vsTableName varchar(50)
select @vsTableName = @TT
select @vsSQL = 'CREATE TABLE ' + @vsTableName + char(10) + '(' + char(10)
select @vsSQL = @vsSQL + ' ' + sc.Name + ' ' +
st.Name +
case when st.Name in ('varchar','varchar','char','nchar') then '(' +
cast(sc.Length as varchar) + ') ' else ' ' end +
case when sc.IsNullable = 1 then 'NULL' else 'NOT NULL' end + ',' + char(10)
from sysobjects so
join syscolumns sc on sc.id = so.id
join systypes st on st.xusertype = sc.xusertype
where so.name = @vsTableName
order by
sc.ColID
select substring(@vsSQL,1,len(@vsSQL) - 2) + char(10) + ')'", con))
{
comQuery.Parameters.AddWithValue("@TT", ListBoxTableNames.SelectedItem);
using (SqlDataReader readerQuery = comQuery.ExecuteReader())
{
QueryOfSelectedTableName.Text="";
while (readerQuery.Read())
{
QueryOfSelectedTableName.Text = readerQuery[0].ToString();
}
}
}
}
}
結果這個 ?
有沒有辦法爲找到我的代碼主鍵和外鍵?我創建並將數據從源數據庫複製到目標數據庫,但目標數據庫表沒有任何主鍵。您可以提供一些提示嗎? – user3107343
我想你可以用sys.index_columns加入。詳細信息在這裏:http://technet.microsoft.com/pl-pl/library/ms173760.aspx和http://technet.microsoft.com/pl-pl/library/ms175105.aspx – semao