创建新的SQL Server表与C# [英] Creating new sql server table with c#

查看:118
本文介绍了创建新的SQL Server表与C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这样的代码来创建新的SQL表
,当我执行此它的展示我这个错误是在屏幕截图。在我的数据库有ISE没有这样的表。它表明这个错误表中的任何名称。谁能帮助我?

I have this code to create new sql table when i execute this its shows me this error which is on screenshot. In my db there ise not such table. it shows this error any name of table. can anyone help me?

public void Create(string TName, string ConString)
    {
        try
        {
            using (SqlCommand cmd = new SqlCommand("CREATE TABLE [dbo].['" + TName + "']("
                            + "[ID] [int] IDENTITY(1,1) NOT NULL,"
                            + "[DateTime] [date] NOT NULL,"
                            + "[BarCode] [nvarchar](max) NOT NULL,"
                            + "[ArtNumber] [nvarchar](max) NOT NULL,"
                            + "[ProductName] [nvarchar](50) NOT NULL,"
                            + "[Quantity] [int] NOT NULL,"
                            + "[SelfPrice] [decimal](18, 2) NOT NULL,"
                            + "[Price] [decimal](18, 2) NOT NULL,"
                            + "[Disccount] [int] NULL,"
                            + "[Comment] [nvarchar](max) NULL,"
                            + "CONSTRAINT ['" + TName + "'] PRIMARY KEY CLUSTERED "
                            + "("
                            + "[ID] ASC"
                            + ")WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]"
                            + ") ON [PRIMARY]", new SqlConnection(ConString)))
            {
                cmd.Connection.Open();
                cmd.ExecuteNonQuery();
                cmd.Connection.Close();
            }
        }
        catch (Exception)
        {

            throw;
        }
    }



推荐答案

您正在使用相同的名称你的表和它的主键。尝试代替约束['PK_+ TNAME +'] PRIMARY KEY CLUSTERED

You're using the same name for your table and its primary key. Try instead "CONSTRAINT ['pk_" + TName + "'] PRIMARY KEY CLUSTERED ".

这篇关于创建新的SQL Server表与C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆