如何使用 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 服务器
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);
}