简体   繁体   English

如何使用SQL查询更改列的身份规范?

[英]How to change identity specification for a column using sql query?

How to change identity specification for a column using sql query ? 如何使用sql查询更改列的身份规范? Only using c# coding. 仅使用c#编码。 Pls Help. 请帮助。 Here i want to add one more column as PaperId while creating new table in following code. 在这里,我想在下面的代码中创建新表时再添加一列作为PaperId。 I need to assign columns property as identity specification yes and identity increment 1. 我需要将列属性分配为标识规范yes和标识增量1。

if (rbtnEng.Checked == true)
{
    con.Open();
    char[] arr = new char[] { 'n', 'g', 'l', 'i', 's', 'h' };
    string str = "CREATE TABLE " + Label1.Text.Trim() + txtpaperset.Text.Trim() 
    + rbtnEng.Text.TrimEnd(arr) + "(" + "quesNo int NOT NULL PRIMARY KEY, " 
    + "question varchar(1000) NOT NULL," + "ansA varchar(500) NOT NULL, " 
    + "ansB varchar(500) NOT NULL, " + "ansC varchar(500) NOT NULL, " 
    + "ansD varchar(500) NOT NULL, " + "rightAns varchar(50) NOT NULL " + ")";
    SqlCommand cmd = new SqlCommand(str, con);
    cmd.ExecuteNonQuery();
    Label2.Text = Label1.Text + txtpaperset.Text + rbtnEng.Text.TrimEnd(arr);
    lblerrormsg.Text = "PaperSet Created Sucessfully!";
    txtpaperset.ReadOnly = true;
    btnpaper.Enabled = false;
    rbtnEng.Enabled = false;
    rbtnMar.Enabled = false;
    UpdatePanel2.Visible = true;
    txtQuestNo.Text = Convert.ToString(1);
    con.Close();
}

else if....

First, this is bad. 首先,这很糟糕。 Don't add so many strings like this; 不要添加太多这样的字符串; use string.Format() . 使用string.Format()

string str = string.Format("CREATE TABLE {0}{1}{2} (" 
+ "quesNo int NOT NULL PRIMARY KEY, question varchar(1000) NOT NULL, " 
+ "ansA varchar(500) NOT NULL, ansB varchar(500) NOT NULL, " 
+ "ansC varchar(500) NOT NULL, ansD varchar(500) NOT NULL, " + 
+ "rightAns varchar(50) NOT NULL )",
Label1.Text.Trim(),
txtpaperset.Text.Trim(),
rbtnEng.Text.TrimEnd(arr));

But that's just my opinion. 但那只是我的个人意见。

Here's how to create an identity column . 这是创建标识列的方法

PaperId int identity(1,1)

Just add it to your str string. 只需将其添加到您的str字符串。

If you just need to add an identity column to the create table script, then here is an example: 如果您只需要在创建表脚本中添加一个标识列,那么下面是一个示例:

create table Data
(
Id int identity(1,1) NOT NULL
)

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

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