2014-03-25 36 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 + ")"; 
相关问题