这是我写的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();
}
}
以下是写的函数,我想问的是,大家这样写过吗,就是通过语句修改表结构,能否像我上面写的语句,请大家帮我看一下,是否正确.
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();
}
}
解决方案 »
- 本来是学java的,但现在还有半个月时间就要去做C#的.net开发
- C# 如何修改浏览器安全级别
- 请各位推荐C#网络编程方面的书(急)
- C#程序启动时,连接到指定的服务器判断是否更新
- 隐藏GridView中的列
- 我现在是一个C#程序员,想问问学习之路
- c# 绘图刷新的问题!
- 我是NHibernate初学者。能帮我看看代码有什么错误吗?很短的代码,但就是出不来。我都快哭了!!:(
- 在某个dataset中,有employee等多个表,怎么有dataset.employeeDataTable dt这种访问方式?
- 朋友们,请问一下,.NET中很多图标都是代表什么意义?
- 我的窗体里加了ico图标,每次退出时就出错误,我想把它去掉,该怎样做?
- 问一个超级初级的问题,莫笑偶
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')";
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";
.....
.....
.....
}
理论是不会出错的 要不就:
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";