谁能帮忙解释下这段代码,谢谢!!最好能详细点
public override DbDataAdapter CreateDataAdapter(string tableName, bool selectOnly){
string key = tableName; // BuildCacheKey(CacheName.DataAdapter, tableName);
DataTable dtSchema = GetTableInfo(tableName);
if (dtSchema.Rows.Count <= 0)
return null;
tableName = string.Format("{0}{1}{2}", LeftBracket, tableName, RightBracket);
SqlConnection conn = CreateConnection() as SqlConnection;
SqlDataAdapter da = new SqlDataAdapter();
if (selectOnly)
{
da.SelectCommand = new SqlCommand(string.Format("SELECT * FROM {0}", tableName), conn);
return da;
}
string colNames = "";
string insertColNames = "";
string insertValues = "";
string paraColNames = "";
string colSets = "";
string pkCondition = "";
string pkOriginalCondition = "";
string afterInsertPkCondition = "";
SqlCommand cmdSelect = conn.CreateCommand();
SqlCommand cmdUpdate = conn.CreateCommand();
SqlCommand cmdInsert = conn.CreateCommand();
SqlCommand cmdDelete = conn.CreateCommand();
string columnName = "";
string dataType = "";
int dataLength = -1;
int dataPrec = 8;
int dataScale = 0;
bool hasPrimaryKey = false;
foreach (DataRow row in dtSchema.Rows)
{
columnName = row["COLUMN_NAME"].ToString();
dataType = row["DATA_TYPE"].ToString();
if (row["DATA_LEN"] != DBNull.Value)
dataLength = Convert.ToInt32(row["DATA_LEN"]);
if (row["DATA_PREC"] != DBNull.Value)
dataPrec = Convert.ToInt32(row["DATA_PREC"]);
if (row["DATA_SCALE"] != DBNull.Value)
dataScale = Convert.ToInt32(row["DATA_SCALE"]);
colNames += string.Format("{0}{1}{2},", LeftBracket, columnName, RightBracket);
paraColNames += string.Format("{0}{1},", ParameterToken, columnName);
if (row["AUTOVAL"].ToString() == "1")
{
afterInsertPkCondition += string.Format("{0}{1}{2}=SCOPE_IDENTITY()", LeftBracket, columnName, RightBracket);
}
else if (row["ISCOMPUTED"].ToString() != "1")
{
insertColNames += string.Format("{0}{1}{2},", LeftBracket, columnName, RightBracket);
insertValues += string.Format("{0}{1},", this.ParameterToken, columnName);
cmdInsert.Parameters.Add(CreateParameter(columnName, dataType, dataLength, dataPrec, dataScale, false));
}
if (row["KEY_SEQ"] != DBNull.Value)
{
hasPrimaryKey = true;
pkCondition += string.Format("{0}{1}{2}={3}{1},", LeftBracket, columnName, RightBracket, ParameterToken);
pkOriginalCondition += string.Format("{0}{1}{2}={3}{4}{1},", LeftBracket, columnName, RightBracket, ParameterToken, OriginalPrefix);
cmdUpdate.Parameters.Add(CreateParameter(columnName, dataType, dataLength, dataPrec, dataScale, true));
cmdDelete.Parameters.Add(CreateParameter(columnName, dataType, dataLength, dataPrec, dataScale, true));
}
else
{
colSets += string.Format("{0}{1}{2}={3}{1},", LeftBracket, columnName, RightBracket, ParameterToken);
cmdUpdate.Parameters.Add(CreateParameter(columnName, dataType, dataLength, dataPrec, dataScale, false));
}
}
if (hasPrimaryKey)
{
colNames = colNames.TrimEnd(',');
colSets = colSets.TrimEnd(',');
insertColNames = insertColNames.TrimEnd(',');
insertValues = insertValues.TrimEnd(',');
pkCondition = pkCondition.TrimEnd(',').Replace(",", " AND ");
pkOriginalCondition = pkOriginalCondition.TrimEnd(',').Replace(",", " AND ");
= string.Format("SELECT {0} FROM {1}", colNames, tableName);
= string.Format("UPDATE {0} SET {1} WHERE {2};SELECT {3} FROM {0} WHERE {2}", tableName, colSets, pkOriginalCondition, colNames);
= string.Format("INSERT INTO {0} ({1}) VALUES({2});SELECT {3} FROM {0} WHERE {4}", tableName, insertColNames, insertValues, colNames, string.IsNullOrEmpty(afterInsertPkCondition) ? pkCondition : afterInsertPkCondition);
= string.Format("DELETE {0} WHERE {1}", tableName, pkOriginalCondition);
da.SelectCommand = cmdSelect;
da.InsertCommand = cmdInsert;
da.DeleteCommand = cmdDelete;
da.UpdateCommand = cmdUpdate;
}
else
{
da.SelectCommand = new SqlCommand(string.Format("SELECT * FROM {0}", tableName), conn);
SqlCommandBuilder cmdBuilder = new SqlCommandBuilder(da);
da.InsertCommand = cmdBuilder.GetInsertCommand(true);
//da.UpdateCommand = cmdBuilder.GetUpdateCommand(true);
//da.DeleteCommand = cmdBuilder.GetDeleteCommand(true);
}
return da;
}