[英]how to add auto increment and primary key in c#
如何使用c#添加自動增量和主鍵?
我使用SQL Server 2008和SQLite
這是我的代碼
使用sqlite
Dictionary<String, String> field = new Dictionary<string, string>();
field.Add("id", "INT");
field.Add("mac", "VARCHAR(25)");
field.Add("serial_number", "VARCHAR(25)");
cDatabaseSQLite.CreateTables("satuan", field);
使用SQL Server
Dictionary<String, String> field = new Dictionary<string, string>();
field.Add("id", "INT");
field.Add("mac", "VARCHAR(25)");
field.Add("serial_number", "VARCHAR(25)");
cDatabaseSQLServer.CreateTables("satuan", field);
編輯
public int CreateTables(String tableName, Dictionary<String, String> data)
{
switch (sqlType)
{
case DATABASE_SQL_TYPE.DATABASE_SQL_TYPE_SQLITE:
return cSQLite.CreateTables(tableName, data);
case DATABASE_SQL_TYPE.DATABASE_SQL_TYPE_MSSQL:
return cSQL.CreateTables(tableName, data);
}
return 0;
}
public int CreateTables(String tableName, Dictionary<String, String> data)
{
string s = "CREATE TABLE " + tableName + " (";
bool first = true;
foreach (KeyValuePair<String, String> val in data)
{
if (first)
{
first = false;
}
else
{
s = s + ",";
}
string s1;
s1 = String.Format("{0} {1}", val.Key, val.Value);
s = s + s1;
}
s = s + ")";
return this.ExecuteNonQuery(s);
}
對於SQL Server,請訪問http://www.w3schools.com/sql/sql_autoincrement.asp ,對於SQLite, 請訪問http://www.tutorialspoint.com/sqlite/sqlite_using_autoincrement.htm
所以,我認為您的代碼必須看起來像這樣
對於SQLite
Dictionary<String, String> field = new Dictionary<string, string>();
field.Add("id", "INTEGER PRIMARY KEY AUTOINCREMENT");
field.Add("mac", "VARCHAR(25)");
field.Add("serial_number", "VARCHAR(25)");
cDatabaseSQLite.CreateTables("satuan", field);
這對於SQL Server
Dictionary<String, String> field = new Dictionary<string, string>();
field.Add("id", "INT PRIMARY KEY IDENTITY");
field.Add("mac", "VARCHAR(25)");
field.Add("serial_number", "VARCHAR(25)");
cDatabaseSQLServer.CreateTables("satuan", field);
在sql server中,關鍵字是PRIMARY KEY和IDENTITY,對於sql lite,關鍵字是PRIMARY KEY AUTOINCREMENT。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.