-2
    public AdvertisementDAL selectAdvertisementLocation1(DateTime now, string gender)
    {
        AdvertisementDAL dal = null;
        string sql = "Select * From Advertisement Where @currentDate between StartDate AND EndDate AND TargetAudience = @gender AND Location = 1";
        SqlConnection conn = new SqlConnection(_connStr);
        SqlCommand cmd = new SqlCommand(sql, conn);
        cmd.Parameters.AddWithValue("@currentDate", now);
        cmd.Parameters.AddWithValue("@gender", gender);
        conn.Open();
        SqlDataReader dr = cmd.ExecuteReader();

        while (dr.Read())
        {
            _advertisementID = int.Parse(dr["AdvertisementID"].ToString());
            _location = int.Parse(dr["Location"].ToString());
            _image = dr["Image"].ToString();
            _targetAudience = dr["TargetAudience"].ToString();
            _url = dr["Url"].ToString();
            _onMouseOverText = dr["OnMouseOverText"].ToString();
            dal = new AdvertisementDAL(_advertisementID, _location, _image, _targetAudience,
                _url, _onMouseOverText);
        }
        conn.Close();
        dr.Close();
        dr.Dispose();
        return dal;
    }

以上是我的 3-tier 代码Data Access Layer。我需要将上面的代码保存到一个XML文件中。我该怎么做呢?

4

2 回答 2

0
<?xml version="1.0" encoding="ISO-8859-1"?>
<code>
  <![CDATA[
public AdvertisementDAL selectAdvertisementLocation1(DateTime now, string gender)
    {
        AdvertisementDAL dal = null;
        string sql = "Select * From Advertisement Where @currentDate between StartDate AND EndDate AND TargetAudience = @gender AND Location = 1";
        SqlConnection conn = new SqlConnection(_connStr);
        SqlCommand cmd = new SqlCommand(sql, conn);
        cmd.Parameters.AddWithValue("@currentDate", now);
        cmd.Parameters.AddWithValue("@gender", gender);
        conn.Open();
        SqlDataReader dr = cmd.ExecuteReader();

        while (dr.Read())
        {
            _advertisementID = int.Parse(dr["AdvertisementID"].ToString());
            _location = int.Parse(dr["Location"].ToString());
            _image = dr["Image"].ToString();
            _targetAudience = dr["TargetAudience"].ToString();
            _url = dr["Url"].ToString();
            _onMouseOverText = dr["OnMouseOverText"].ToString();
            dal = new AdvertisementDAL(_advertisementID, _location, _image, _targetAudience,
                _url, _onMouseOverText);
        }
        conn.Close();
        dr.Close();
        dr.Dispose();
        return dal;
    }
]]>
</code>

Presto.. XML 格式的 C#。

于 2013-01-29T04:23:44.487 回答
0
public AdvertisementDAL selectAdvertisementLocation1(DateTime now, string gender)
    {
        AdvertisementDAL dal = null;
        string sql = "Select * From Advertisement Where @currentDate between StartDate AND EndDate AND TargetAudience = @gender AND Location = 1";
        SqlConnection conn = new SqlConnection(_connStr);
        SqlCommand cmd = new SqlCommand(sql, conn);
        cmd.Parameters.AddWithValue("@currentDate", now);
        cmd.Parameters.AddWithValue("@gender", gender);
        conn.Open();
        SqlDataReader dr = cmd.ExecuteReader();

    while (dr.Read())
    {
        _advertisementID = int.Parse(dr["AdvertisementID"].ToString());
        _location = int.Parse(dr["Location"].ToString());
        _image = dr["Image"].ToString();
        _targetAudience = dr["TargetAudience"].ToString();
        _url = dr["Url"].ToString();
        _onMouseOverText = dr["OnMouseOverText"].ToString();
        dal = new AdvertisementDAL(_advertisementID, _location, _image, _targetAudience,
            _url, _onMouseOverText);
       System.IO.StreamWriter file = new System.IO.StreamWriter(
    @"c:\temp\SerializationOverview.xml"); //rmb to create the file
writer.Serialize(file, dal);
file.Close();
    }
    conn.Close();
    dr.Close();
    dr.Dispose();
    return dal;
}
于 2013-01-29T05:02:58.087 回答