未处理 System.Data.SqlClient.SqlException Message=数据库中已存在名为 'hy_Linshibiao' 的对象

haoziran 2017-08-02 03:46:51
private void listBox1_Click(object sender, EventArgs e)
{
string strTableName = this.listBox1.SelectedValue.ToString();
using(SqlConnection conn=new SqlConnection("server=localhost;database='"+strDatabase+"';uid=sa;pwd=chy19900909"))
{
错在这里,将string strSql;先声明,后使用
string strSql= "select name 字段名, xusertype 类型编号, length 长度 into hy_Linshibiao from syscolumns where id=object_id('" + strTableName + "') ";
strSql += "select name 类型,xusertype 类型编号 into angel_Linshibiao from systypes where xusertype in (select xusertype from syscolumns where id=object_id('" + strTableName + "'))";
conn.Open();
SqlCommand cmd = new SqlCommand(strSql, conn);
cmd.ExecuteNonQuery();
conn.Close();

SqlDataAdapter da = new SqlDataAdapter("select 字段名,类型,长度 from hy_Linshibiao t,angel_Linshibiao b where t.类型编号=b.类型编号", conn);
DataTable dt = new DataTable();
da.Fill(dt);
this.dataGridView1.DataSource = dt.DefaultView;

SqlCommand cmdnew = new SqlCommand("drop table hy_Linshibiao,angel_Linshibiao", conn);
conn.Open();
cmdnew.ExecuteNonQuery();
conn.Close();

}

}
...全文
225 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
songdagege 2017-11-29
  • 打赏
  • 举报
回复
学习了,确实是这样的。多多指导
xdashewan 2017-08-02
  • 打赏
  • 举报
回复
引用 3 楼 haoziran 的回复:
问题不在这,在于红色部分,string strsql;这个先声明,然后再用 strsql= "select name 字段名, xusertype 类型编号, length 长度 into hy_Linshibiao from syscolumns where id=object_id('" + strTableName + "') ";
你后面那句把两句select拼在一起又是怎么回事?
haoziran 2017-08-02
  • 打赏
  • 举报
回复
问题不在这,在于红色部分,string strsql;这个先声明,然后再用 strsql= "select name 字段名, xusertype 类型编号, length 长度 into hy_Linshibiao from syscolumns where id=object_id('" + strTableName + "') ";
xdashewan 2017-08-02
  • 打赏
  • 举报
回复
就是说表已经有了,不drop掉就别建同名的了
haoziran 2017-08-02
  • 打赏
  • 举报
回复

110,535

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧