对之前.net项目数据库操作方面的使用总结

时间:2021-05-08 06:09:08
//数据库的操作
SqlDataReader myReader = null;
SqlCommand sqlCommand = null;
string sqlText = string.Empty;//或者StringBuilder及其方法append
try
{
sqlCommand = new SqlCommand(sqlText, sqlConnection);

sqlText += "SELECT" + Environment.NewLine;
sqlText += " Table1.ENTERPRISECODERF" + Environment.NewLine;
sqlText += " ,Table2.LOGICALDELETECODERF" + Environment.NewLine;
sqlText += " FROM Table1 WITH (READUNCOMMITTED)" + Environment.NewLine;
sqlText += " LEFT JOIN Table2" + Environment.NewLine;
sqlText += " ON Table2.ENTERPRISECODERF = Table1.ENTERPRISECODERF" + Environment.NewLine;
sqlText += " WHERE " + Environment.NewLine;
sqlText += " Table1.ENTERPRISECODERF=@ENTERPRISECODE " + Environment.NewLine;
sqlText += " AND Table1.LOGICALDELETECODERF=0 " + Environment.NewLine;

SqlParameter paraEnterpriseCode = sqlCommand.Parameters.Add("@ENTERPRISECODE", SqlDbType.NChar);
paraEnterpriseCode.Value = SqlDataMediator.SqlSetString(extrInfo.EnterpriseCode);
sqlCommand.CommandText = sqlText;

myReader = sqlCommand.ExecuteReader();

while (myReader.Read())
{
retList.Add(CopyTo(ref myReader));
//CopyTo方法中将读取的数据赋值到一个类中
//extrInfo.EnterpriseCode =
//SqlDataMediator.SqlGetString(myReader, myReader.GetOrdinal("ENTERPRISECODERF"));
}
}
catch (SqlException ex)
{
}
finally
{
if (sqlCommand != null) sqlCommand.Dispose();
if (myReader != null)
if (!myReader.IsClosed) myReader.Close();
}