实现数据访问层并连接到数据库?

时间:2015-06-26 02:57:15

标签: c# sql .net c#-4.0

一周和4天的C#经验,请耐心等待。
我被分配了构建一个将信息存储到连接数据库的webform的任务。我创建了一个数据访问层类,我想连接一个人选择编辑/创建新条目后将显示的代码。

我希望DAL做3件事:

  1. View它将更新的表格
  2. 在表中添加记录,即insert
  3. Update上一个条目中的字段
  4. 以下是我到目前为止的代码:

    public class AccountTrackerDAL
    { #region View ConnectionType()
      public DataTable ViewConnectionType()
       {  
          var sqlStatement = new StringBuilder();
    
          sqlStatement.Append(" SELECT");
          sqlStatement.Append(" ConnectionTypeID, ConnectionTypeDesc, CreatedBy, CreatedDate, LastUpdatedBy, LastUpdatedDate");
          sqlStatement.Append("  FROM");
          sqlStatement.Append("  dbo.ConnectionType");
          sqlStatement.Append("  ORDER BY");
          sqlStatement.Append("  ConnectionTypeDes");
    
           SqlCommand sqlCmd = new SqlCommand(sqlStatement.ToString());
           return DBAccess.SQLServer.GetDataTable(DBAccess.SQLServer.GetConnectionString("AccountTrackerDB",sqlCmd);
       }#endregion // This code has no errors and does what I want.
    
        #region View ConnectionType by Id
    
          public DataTable ViewConnectionTypeByID(int connectionTypeID)
          { var sqlStatement = new StringBuilder();
    
            sqlStatement.Append(" SELECT");
            sqlStatement.Append("   ConnectionTypeID, ConnectionTypeDesc, Created By, CreatedDate, LastUpdatedBy, LastUpdatedDate");
            sqlStatement.Append(" FROM");
            sqlStatement.Append( "   dbo.ConnectionType");
            sqlStatement.Append("  WHERE");
            sqlStatement.Append(" ConnectionTypeID = @ConnectionTypeID");
    
            SqlCommand sqlCommand = new SqlCommand(sqlStatement.ToString());
            sqlCommand.Parameters.Add("@ConnectionTypeID", SqlDbType.Int);
            sqlCommand.Parameters["@ConnectionTypeID"].Value = connectionTypeID;
    
            return DBAccess.SQLServer.GetDataTable(DBAccess.SQLServer.GetConnectionSTring("AccountTracker"),sqlCommand);
          }#endregion // this code also functions as i need it to, according to //my boss. Although I don't understand it.
    
    #region Insert ConnectionType by Id /* I need this method to pass a value when the user chooses to not a hard coded value. In other words i just need to pass a place holder. Here is where i don't know what to pass */
           public DataTable UpdateConnectionTypeById(int connectionTypeID)
           { var sqlStatement = new StringBuilder();
    
             sqlStatement.Append("INSERT INTO");
             sqlStatement.Append("  ConnectionType"); 
             sqlStatement.Append(" WHERE");
             sqlStatement.Append(" ConnecitonTypeID = @ConnectionID"); 
    
             SqlCommand sqlCommand = new SqlCommand(sqlStatement.ToString());
             sqlCommand.Parameters.Add("@ConnectionTypeid"), SqlDbType.Int);
             sqlCommand.Parameters[@ConnectionTypeid].Value = connectionTypeID;
    
             return DBAccess.SQLServer.GetDataTable(DBAccess.SQLServer.GetConnectionString("AccounTracker"),sqlCommand);
           }#endregion
    
    }
    

    我会尽力回答任何人可能提出的问题。感谢。

0 个答案:

没有答案