Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. 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;
        }
    }

alt text

like image 687
Irakli Lekishvili Avatar asked Dec 27 '10 18:12

Irakli Lekishvili


People also ask

How do I create a new table in SQL Server?

In SSMS, in Object Explorer, connect to the instance of Database Engine that contains the database to be modified. In Object Explorer, expand the Databases node and then expand the database that will contain the new table. In Object Explorer, right-click the Tables node of your database and then click New Table.

Can you use SQL with C?

You can code SQL statements in a C or C++ program wherever you can use executable statements. Each SQL statement in a C or C++ program must begin with EXEC SQL and end with a semicolon (;). The EXEC and SQL keywords must appear on one line, but the remainder of the statement can appear on subsequent lines.

What is C in SQL query?

C is an alias for the results from the sub-query (select min(A. bidvalue) as ....). This subquery will product a result set which behaves like a table for the duration of the query. To refer to this result set and its columns, it was given the alias name "C" and all C.

Is C similar to SQL?

To undestand the difference between a language like C and SQL is that basically SQL is a specialized type of language that is concerned with database operations. C is less concerned with accessing the data than with how the whole application will work.


1 Answers

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

like image 164
MarkXA Avatar answered Sep 26 '22 17:09

MarkXA