私は、データベース接続を開き、そこからデータを取得してユーザーに表示するアプリケーションを開発中です。私が見ることのできるコードではすべてがうまく見えますが、以下のメソッドで例外がスローされるため、何かが間違っています。私は "conn.Open();"にブレークポイントを設定しました。例外がスローされます。何が間違っていた(例外)かを確認するには?
私は実際のエラーの内容と解決方法を知りません。ここで助けを得ることを望みます。例外のスタックトレースは、役に立つ場合はhereとなります。
例外がある:System.ApplicationException:アプリケーションでエラーが発生しました
おかげで事前に!
例外をスローする方法:web.configファイルから
public abstract class DALBase {
private static string _connectionString;
static DALBase() {
_connectionString = WebConfigurationManager.ConnectionStrings["NameOfTheDatabase_ConnectionString"].ConnectionString;
}
protected SqlConnection CreateConnection() {
return new SqlConnection(_connectionString);
}
}
:上記の方法を含むクラスの
public List<Movie> GetMovies() {
var movieTitles = new List<Movie>(100);
using (var conn = CreateConnection()) {
try {
var cmd = new SqlCommand("dbo.usp_GetMovies", conn);
cmd.CommandType = CommandType.StoredProcedure;
conn.Open();
using (var reader = cmd.ExecuteReader()) {
var movieIDIndex = reader.GetOrdinal("MovieID");
var nameIndex = reader.GetOrdinal("Name");
var yearIndex = reader.GetOrdinal("Year");
var lengthIndex = reader.GetOrdinal("Length");
var summaryIndex = reader.GetOrdinal("Summary");
while (reader.Read()) {
movieTitles.Add(new Movie {
MovieID = reader.GetInt32(movieIDIndex),
Name = reader.GetString(nameIndex),
Year = reader.GetInt32(yearIndex),
Length = reader.GetInt32(lengthIndex),
Summary = reader.GetString(summaryIndex),
});
}
}
}
catch {
throw new ApplicationException("An error occured!");
}
}
movieTitles.TrimExcess();
return movieTitles;
}
相続人基底クラス
<connectionStrings>
<add name="NameOfTheDatabase_ConnectionString" connectionString="Data Source=xxx.xx.xxx.x;Initial Catalog=The_Catalog;User ID=xxxxxx;Password=xxxxxx" providerName="System.Data.SqlClient"/>
</connectionStrings>
接続を開く時点で例外がスローされる場合は、接続文字列に問題があると思われます。接続文字列が有効であることを確認するには、VS2010接続ダイアログを開き、ダイアログを使用して文字列を作成します。あなたが接続することができれば、それはこれを排除します - あなたができなければ、接続文字列は有効ではなく、あなたの答えがあります。 – MoonKnight