2014-03-25 32 views
0

我想創建表。如何通過提供寬度選項來創建表格?

下面是我的代碼:

private void btnOK_Click(object sender, EventArgs e) 
{ 
    if (con.State == ConnectionState.Open) { con.Close(); } 
    con.Open(); 
    string s = "CREATE TABLE ["+"" + rchtxtFieldCode.Text + "] "+ " (" + rchFieldTitle.Text + " " + combDataType.Text + "("+txtWidth.Text+")" + ")"; 
    SqlCommand cmd = new SqlCommand(s, con); 
    if (cmd.ExecuteNonQuery() >= 1) 
    { 
     MessageBox.Show("created"); 
    } 
    con.Close(); 
} 

它正在創建表。但是,當數據類型是int或text時,它顯示異常。

我希望每個數據類型都能正常工作。

+0

異常的類型及其投擲? – Neel

+0

添加條件來處理int和沒有寬度的文本? –

+0

「不正確的語法附近')'。」 - 當沒有在txtWidth.Text中指定任何內容時 – user3377879

回答

0

試試這個

string width = string.IsNullOrEmpty(txtWidth.Text.Trim()) ? string.Empty : "(" + txtWidth.Text.Trim() + ")" 
string s = "CREATE TABLE [" + "" + rchtxtFieldCode.Text + "] " + " (" + rchFieldTitle.Text + " " + combDataType.Text + width + ")"; 
相關問題