2014-02-08 53 views
0

我有一個查詢從C#中,如何在文本框中顯示「腳本表作爲創建查詢」的結果?

查詢:

declare @vsSQL varchar(8000) 
    declare @vsTableName varchar(50) 
    select @vsTableName = 'dbo.MyTableName' 
    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) + ')' 

查詢的結果:enter image description here

我試着在listBox1_SelectedIndexChanged在列表框中的文本框的foreach選擇的項目申請此查詢顯示

 private void listBox1_SelectedIndexChanged(object sender, EventArgs e) 

    { 

     using (SqlConnection con = new SqlConnection(strConnect)) 

    { 

    con.Open(); 


    using (SqlCommand comQuery = new SqlCommand(@" declare @vsSQL varchar(8000) 
    declare @vsTableName varchar(50) 
    select @vsTableName = '@SelectedTableFormListBox' 
    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("@SelectedTableFormListBox", listBox1.SelectedItem); 

    using (SqlDataReader readerQuery = comQuery.ExecuteReader()) 

    { 

    textBox1.Text = ""; 


    while (readerQuery.Read()) 

    { 

    textBox1.Text = ?? 

    } 


    } 

    } 

    } 

    } 

我的問題:我不確定使用關於選擇@vsTableName ='@SelectedTableFor mListBox'

我不知道如何顯示在文本框中。

回答

0

我解決了。

變化

select @vsTableName = '@SelectedTableFormListBox' 

select @vsTableName = @SelectedTableFormListBox 

變化

 while (readerQuery.Read()) 

     { 

     textBox1.Text = ?? 

     } 

 while (readerQuery.Read()) 

     { 

     textBox1.Text = readerQuery[0].ToString() ; 

     } 
相關問題