简体   繁体   English

用c#创建新的sql server表

[英]Creating new sql server table with c#

I have this code to create new sql table when i execute this its shows me this error which is on screenshot. 我有这个代码来创建新的SQL表,当我执行它时它显示我在屏幕截图上的这个错误。 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;
        }
    }

替代文字

You're using the same name for your table and its primary key. 您为表及其主键使用相同的名称。 Try instead "CONSTRAINT ['pk_" + TName + "'] PRIMARY KEY CLUSTERED " . 请改为"CONSTRAINT ['pk_" + TName + "'] PRIMARY KEY CLUSTERED "

The error message doesn't seem to be related to the name of the table. 该错误消息似乎与表的名称无关。 It seems related to the name of the constraint. 它似乎与约束的名称有关。 It looks like you are naming a constraint 'beso' and another object by that name already exists in your DB. 看起来您正在命名一个约束'beso',并且该名称中的另一个对象已经存在于您的数据库中。

Instead of fighting with SQL syntax, you could also use Mig# like this: 您也可以像这样使用Mig# ,而不是使用SQL语法。

        var schema = new DbSchema(ConnectionString, DbPlatform.SqlServer2014);
        schema.Alter(db => db.CreateTable(TName)
           .WithPrimaryKeyColumn("Id", DbType.Int32).AsIdentity()
           .WithNotNullableColumn("DateTime", DbType.Date)
           ...);

The only down-side of this approach being that Mig# is not suitable to use very SQL Server specific features as it supports many platforms in a portable way. 这种方法的唯一缺点是Mig#不适合使用非常SQL Server特定的功能,因为它以可移植的方式支持许多平台。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM