『壹』 sql Server 中怎樣修改表中的列名
1、可以在SQL sever中用設計表,直接更改
2、如果是別名的話,可以在查詢的欄位名後加 AS + 想加的名字
如:SELECT name AS 名字 FROM SANG
3、可以通過修改表來實現,ALTER TABLE sang 命令進行修改sang為表名
『貳』 .NET多個用戶對oracle資料庫的同一張表進行插入,發現有的用戶出現插入不進的情況,怎麼解決加鎖怎麼加
可以用樂觀鎖,在表上面加個欄位rowversion,假如初值是1
當一個用戶去更新表時,
1)先獲取這個欄位的值1
2)然後更新數據,rowversion+1,
3)提交時,先判斷rowversion是否等於1,
如果不等於,說明有別的用戶已經更改過該行數據了。就從1)開始在做一遍。
如果等於,就提交(此時rowversion=2)
你可以查查樂觀鎖的相關知識。
『叄』 c# 求通用的資料庫添加方法
這個是我自己寫的,通配資料庫所以表
先初始化資料庫所有表結構
public static string connectionString;
private static List<Column> columns;
private static Dictionary<string, SqlDbType> sqlDbType;
static SqlHelper()
{
sqlDbType = new Dictionary<string, SqlDbType>();
sqlDbType.Add("bigint", SqlDbType.BigInt);
sqlDbType.Add("binary", SqlDbType.VarBinary);
sqlDbType.Add("bit", SqlDbType.Bit);
sqlDbType.Add("char", SqlDbType.Char);
sqlDbType.Add("date", SqlDbType.Date);
sqlDbType.Add("datetime", SqlDbType.DateTime);
sqlDbType.Add("datetime2", SqlDbType.DateTime2);
sqlDbType.Add("datetimeoffset", SqlDbType.DateTimeOffset);
sqlDbType.Add("decimal", SqlDbType.Decimal);
sqlDbType.Add("float", SqlDbType.Float);
sqlDbType.Add("image", SqlDbType.Binary);
sqlDbType.Add("int", SqlDbType.Int);
sqlDbType.Add("money", SqlDbType.Money);
sqlDbType.Add("nchar", SqlDbType.NChar);
sqlDbType.Add("ntext", SqlDbType.NText);
sqlDbType.Add("numeric", SqlDbType.Decimal);
sqlDbType.Add("nvarchar", SqlDbType.NVarChar);
sqlDbType.Add("real", SqlDbType.Real);
sqlDbType.Add("rowversion", SqlDbType.Timestamp);
sqlDbType.Add("smalldatetime", SqlDbType.DateTime);
sqlDbType.Add("smallint", SqlDbType.SmallInt);
sqlDbType.Add("smallmoney", SqlDbType.SmallMoney);
sqlDbType.Add("sql_variant", SqlDbType.Variant);
sqlDbType.Add("text", SqlDbType.Text);
sqlDbType.Add("time", SqlDbType.Time);
sqlDbType.Add("timestamp", SqlDbType.Timestamp);
sqlDbType.Add("tinyint", SqlDbType.TinyInt);
sqlDbType.Add("uniqueidentifier", SqlDbType.UniqueIdentifier);
sqlDbType.Add("varbinary", SqlDbType.VarBinary);
sqlDbType.Add("varchar", SqlDbType.VarChar);
sqlDbType.Add("xml", SqlDbType.Xml);
columns = new List<Column>();
DataTable dt = ExecuteDataset("SELECT d.name AS c_table, a.colorder AS c_OrderID, a.name AS c_name, a.colstat AS c_IsIdentity, CASE WHEN EXISTS (SELECT 1 FROM sysindexes y, sysindexkeys z WHERE y.id = z.id AND y.indid = z.indid AND z.id = a.id AND z.colid = a.colid AND y.status & 2948 = 2048) THEN '1' ELSE '0' END AS c_IsPK, b.name AS c_type, a.length AS c_ByteNumber, a.prec AS c_Length, ISNULL(a.scale, 0) AS c_DecimalDigits, a.isnullable AS c_IsAllowNull, ISNULL(e.text, '') AS c_Default, ISNULL(g.[value], '') AS c_Description FROM syscolumns a LEFT JOIN systypes b ON a.xusertype = b.xusertype INNER JOIN sysobjects d ON a.id = d.id AND d.xtype = 'U' AND d.name <> 'dtproperties' LEFT JOIN syscomments e ON a.cdefault = e.id LEFT JOIN sys.extended_properties g ON a.id = G.major_id AND a.colid = g.minor_id LEFT JOIN sys.extended_properties f ON d.id = f.major_id AND f.minor_id = 0 WHERE d.name <> 'sysdiagrams' ORDER BY a.id,a.colorder").Tables[0];
foreach (DataRow dr in dt.Rows)
{
Column model = new Column();
model.Table = dr["c_table"].ToString();
model.OrderID = int.Parse(dr["c_OrderID"].ToString());
model.Name = dr["c_name"].ToString();
model.IsIdentity = (dr["c_IsIdentity"].ToString() == "1" ? true : false);
model.IsPK = (dr["c_IsPK"].ToString() == "1" ? true : false);
model.SqlDbType = sqlDbType[dr["c_type"].ToString()];
model.ByteNumber = int.Parse(dr["c_ByteNumber"].ToString());
if (dr["c_Length"].ToString() == "")
{
model.Length = null;
}
else
{
model.Length = int.Parse(dr["c_Length"].ToString());
}
model.DecimalDigits = int.Parse(dr["c_DecimalDigits"].ToString());
model.IsAllowNull = (dr["c_IsAllowNull"].ToString() == "1" ? true : false);
model.Default = dr["c_Default"].ToString();
model.Description = dr["c_Description"].ToString();
columns.Add(model);
}
}
/// <summary>
/// 數據添加
/// </summary>
public static object Add<T>(T dbModel)
{
using (SqlConnection connection = new SqlConnection(connectionString))
{
using (SqlCommand cmd = new SqlCommand())
{
if (connection.State != ConnectionState.Open)
{
connection.Open();
}
cmd.Connection = connection;
Type t = typeof(T);
PropertyInfo[] pros = t.GetProperties();
bool isIdentity = false;
StringBuilder sql = new StringBuilder(100);
foreach (PropertyInfo pro in pros)
{
Column model = columns.Find(m => m.Table == t.Name && m.Name == pro.Name);
if (model.IsPK && model.IsIdentity)
{
isIdentity = true;
}
if (!model.IsIdentity)
{
sql.Append(model.Name + ',');
object val = pro.GetValue(dbModel, null);
if (val == null)
{
val = DBNull.Value;
}
SqlParameter sp = new SqlParameter("@" + model.Name, model.SqlDbType);
sp.Value = val;
cmd.Parameters.Add(sp);
}
}
cmd.CommandText = "INSERT INTO " + t.Name + "(" + sql.ToString().TrimEnd(',') + ") VALUES (@" + sql.Replace(",", ",@").ToString().TrimEnd(new char[] { ',', '@' }) + ")" + (isIdentity ? ";SELECT @@IDENTITY" : "");
cmd.CommandType = CommandType.Text;
object obj = cmd.ExecuteScalar();
cmd.Parameters.Clear();
if (Object.Equals(obj, System.DBNull.Value))
{
return null;
}
else
{
return obj;
}
}
}
}