Pregunta

    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;
    }

Above is my code for my 3-tier Data Access Layer. I need to save the above code into an XML file. How do I do this?

¿Fue útil?

Solución

<?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.. C# in XML format.

Otros consejos

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;
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top