当前位置:编程学习 > C#/ASP.NET >>

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





using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        string strDatabase = null;
        public Form1()
        {
            InitializeComponent();
        }

        private DataTable Database()
        {
            using(SqlConnection conn=new SqlConnection("server=localhost;database=master;uid=sa;pwd=chy19900909"))
            {
                SqlDataAdapter da = new SqlDataAdapter("select name from sysdatabases",conn);
                DataTable dt = new DataTable("sysdatabases");
                da.Fill(dt);
                return dt;
            }
        }
        private void Form1_Load(object sender, EventArgs e)
        {
            comboBox1.DataSource = Database();
            comboBox1.DisplayMember = "name";
            comboBox1.ValueMember = "name";
        }

        private void button1_Click(object sender, EventArgs e)
        {
            this.dataGridView1.DataSource=null;
            strDatabase = this.comboBox1.Text.ToString();
            using(SqlConnection conn=new SqlConnection("server=localhost;database='"+strDatabase+"';uid=sa;pwd=chy19900909"))
            {
                SqlDataAdapter da = new SqlDataAdapter("select name from sysobjects where type='U' and name<>'dtproperties'",conn);
                DataTable dt = new DataTable("sysobjects");
                da.Fill(dt);
                listBox1.DataSource = dt.DefaultView;
                listBox1.DisplayMember = "name";
                listBox1.ValueMember="name";

            }

        }

      
        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 = "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();
            
            }

        }

       
     
    }
}
数据库 database object string server --------------------编程问答-------------------- select * ... into angel_Linshibiao ...
这样会创建angel_Linshibiao表,你已经存在angel_Linshibiao表了,所以改成insert into angel_Linshibiao select ...

 strSql += "insert into angel_Linshibiao select name 类型,xusertype 类型编号 from systypes where  --------------------编程问答-------------------- IF OBJECT_ID('angel_Linshibiao')IS NOT NULL 
DROP TABLE angel_Linshibiao  --------------------编程问答--------------------
引用 1 楼 gxingmin 的回复:
select * ... into angel_Linshibiao ...
这样会创建angel_Linshibiao表,你已经存在angel_Linshibiao表了,所以改成insert into angel_Linshibiao select ...

 strSql += "insert into angel_Linshibiao select name ……


正解
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,