| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 658 人关注过本帖
标题:谁能帮忙解释下这段代码,谢谢!!最好能详细点
只看楼主 加入收藏
lvyhong
Rank: 1
等 级:新手上路
帖 子:93
专家分:0
注 册:2008-11-10
结帖率:81.25%
收藏
 问题点数:0 回复次数:5 
谁能帮忙解释下这段代码,谢谢!!最好能详细点
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;
  }
 
搜索更多相关主题的帖子: null 
2011-01-18 15:31
lvyhong
Rank: 1
等 级:新手上路
帖 子:93
专家分:0
注 册:2008-11-10
收藏
得分:0 
求助~~~~~高手!高手!!
2011-01-18 16:24
chen41345507
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2011-3-4
收藏
得分:0 
悲剧
2011-03-04 16:38
xydddaxia
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:贵宾
威 望:33
帖 子:466
专家分:2307
注 册:2009-3-20
收藏
得分:0 
太长了,没得哪个解释

站在春哥的肩膀上
2011-03-04 18:02
zkmhywgsym
Rank: 2
等 级:论坛游民
帖 子:38
专家分:98
注 册:2011-3-5
收藏
得分:0 
看不懂,不过怎么不见高手回啊,不可能都是菜鸟吧
2011-03-05 20:14
graper
Rank: 1
等 级:新手上路
帖 子:1
专家分:0
注 册:2008-10-30
收藏
得分:0 
我看懂了个大概。。。
2011-03-30 21:17
快速回复:谁能帮忙解释下这段代码,谢谢!!最好能详细点
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.085808 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved