0

私はc#で以下のコードを持っています。

SqlConnection conn = new SqlConnection("Data Source=MANOJ-PC\\SQLEXPRESS;Initial Catalog=master;Integrated Security=False;User Id=sa;Password=Manoj;");
conn.Open();

if (conn != null)
{
    //create command
    SqlCommand cmd = new SqlCommand("dbo.GETTridionLinkData", conn);
    cmd.Parameters.AddWithValue("@PageID", "637518");
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.CommandTimeout = 500;
    StringBuilder sbXML = new StringBuilder();
    //Adding Root node
    sbXML.Append("<TridionLinks>");

    //Reading all the values of Stored procedure return
    using (XmlReader reader = cmd.ExecuteXmlReader())
    {
        while (reader.Read())
        {
            sbXML.Append(reader.ReadOuterXml().Replace("//", "/"));
        }
    }

    //Closing the root node tag
    sbXML.Append("</TridionLinks>");
    XmlDocument xDoc = new XmlDocument();

    //Loading string xml in XML Document
    xDoc.LoadXml(sbXML.ToString());
}

上記のコードでは、次のことがわかります。cmd.CommandTimeout=500に設定しました。、タイムアウトがこれより長い場合、またはデータベースがダウンしていると言うことができる場合は、ユーザーにエラーメッセージを表示したいと思います。

提案してください!!

4

1 に答える 1

1

SQLServer タイムアウト例外をキャッチする方法を参照してください。

質問はすでに回答されています..

コーディングを改善するには、次を使用できます

    試す{

         using (sqlconnection Conn = new SqlConnection("Data Source=MANOJ-PC\\SQLEXPRESS;Initial Catalog=master;Integrated Security=False;User Id=sa;Password=Manoj;"){
            ...
        }
    }catch(sqlException ex){
       if (ex.Number == -2) {
        // コントロールにメッセージを返すか、エラーを表示します
       }
    }

ほんの一例です..

于 2012-03-02T11:33:03.283 に答える