搜尋此網誌

2011-12-04

SqlConnection

string Result = "";
using (SqlConnection Conn = new SqlConnection())
{
    //DB連線字串
    Conn.ConnectionString = WebConfigurationManager.ConnectionStrings["ASPNETDB"].ConnectionString;
    //SQL Command
    string SqlStr = @"INSERT INTO WebRssPath (WebName, WebRssXmlPath) VALUES (@WebName, @WebRssXmlPath)";

    SqlCommand cmd = new SqlCommand(SqlStr, Conn);
    //SQL Parameter
    cmd.Parameters.AddWithValue("@WebName", WebName);
    cmd.Parameters.AddWithValue("@WebRssXmlPath", WebRssXmlPath);

    //開啟連線
    Conn.Open();

    try
    {
        cmd.ExecuteNonQuery();
        Result = "寫入成功";
    }
    catch (Exception ex)
    {
        Result = ex.Message.ToString() + " / " + ex.StackTrace.ToString();
    }
    finally
    {
        //關閉連線
        cmd.Dispose();
        Conn.Close();
        Conn.Dispose();
    }

}
return Result;


KB-當心SqlDataReader.Close時的額外資料傳輸量

沒有留言: