当前位置:首页 » 编程语言 » sqlrowversion
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

sqlrowversion

发布时间: 2023-08-30 22:04:25

‘壹’ 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;
}
}
}
}