这是我写的SQL语句:private const string SQL_INSERT_COLUMNS = "alter table ss_ztsz add @ColumnName @DataType(@Width) @isnull";
以下是写的函数,我想问的是,大家这样写过吗,就是通过语句修改表结构,能否像我上面写的语句,请大家帮我看一下,是否正确.
public bool Add_Column(string strname, string strdatatype,string strwidth,string strisnull)
{
//建立参数并赋值
SqlParameter[] param = new SqlParameter[4]; param[0] = new SqlParameter("@ColumnName", SqlDbType.VarChar,50);
param[0].Value = strname;
                    
param[1] = new SqlParameter("@DataType",SqlDbType.VarChar,20);
param[1].Value = strdatatype; param[2] = new SqlParameter("@Width", SqlDbType.VarChar,50);
param[2].Value = strwidth; param[3] = new SqlParameter("@isnull",SqlDbType.VarChar,20);
param[3].Value = strisnull;

try
{ SqlData.ExecuteNonQuery(ConnectionString,CommandType.Text,SQL_INSERT_COLUMNS,param);
                                return true;
}
catch(SqlException ex)
{
MessageBox.Show("数据库错误:" + ex.Message );
return false;
}
finally
{
ConnectionString.Close();
}

}

解决方案 »

  1.   

    sorry ,不懂,帮你顶顶~~~~~
      

  2.   

    或:
    private const string SQL_INSERT_COLUMNS = "alter table ss_ztsz add @ColumnName @DataType(@Width) @isnull";->
    private const string SQL_INSERT_COLUMNS = "Execute('alter table ss_ztsz add '+@ColumnName + @DataType+'('+@Width+') @isnull')";
      

  3.   

    放到函數裡看看
    public bool Add_Column(string strname, string strdatatype,string strwidth,string strisnull)
    {
      string SQL_INSERT_COLUMNS = "alter table ss_ztsz add @ColumnName @DataType(@Width) @isnull";
      .....
      .....
      .....
    }
      

  4.   

    你不能用参数来直接代替来作为sql关键字的变量
      

  5.   

    你用存储中去做,然后用exec来执行是没有问题的。
      

  6.   

    看一下你打算加入的字段类型 string strdatatype strDataType的值是什么?
    理论是不会出错的 要不就:
    private const string SQL_INSERT_COLUMNS = "alter table ss_ztsz add @ColumnName @DataType(@Width) @isnull";
    改成:
    private const string SQL_INSERT_COLUMNS = "alter table ss_ztsz add @ColumnName @DataType(@Width) @isnull  Go";