VS2005 C#ASP.NETとSQL Server 2005を使用しています。インポートする前にファイルの内容を確認する
Excelデータをインポートする機能があります。私は、内部のデータが不適切な状況に遭遇したとき、SQL Server DBを停止させます。
など。 SELECT [Username]、[Password] from [userlist $] - > Excelスプレッドシートに[Username]より大きい値が列またはその列の下に含まれていると、サーバーがクラッシュします。
E.G.
アップロードする前にこのファイルのエラーを確認するにはどうすればよいですか?確認のためif
else
のステートメントを推奨します。
ご協力いただきありがとうございます。以下は
は、Excelのアップロードのための私のコードスニペットです:if (FileImport.HasFile)
{
// Get the name of the Excel spreadsheet to upload.
string strFileName = Server.HtmlEncode(FileImport.FileName);
// Get the extension of the Excel spreadsheet.
string strExtension = Path.GetExtension(strFileName);
// Validate the file extension.
if (strExtension == ".xls" || strExtension == ".xlsx")
{
// Generate the file name to save.
string strUploadFileName = "C:/Documents and Settings/user01/My Documents/Visual Studio 2005/WebSites/MajorProject/UploadFiles/" + DateTime.Now.ToString("yyyyMMddHHmmss") + strExtension;
// Save the Excel spreadsheet on server.
FileImport.SaveAs(strUploadFileName);
// Create Connection to Excel Workbook
string connStr = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + strUploadFileName + ";Extended Properties=Excel 8.0;";
using (OleDbConnection connection =
new OleDbConnection(connStr))
{
string selectStmt = string.Format("Select [COLUMNS] FROM [userlist$]");
OleDbCommand command = new OleDbCommand(selectStmt, connection);
connection.Open();
Console.WriteLine("Connection Opened");
// Create DbDataReader to Data Worksheet
using (DbDataReader dr = command.ExecuteReader())
{
// SQL Server Connection String
string sqlConnectionString = "Data Source=<datasource>";
// Bulk Copy to SQL Server
using (SqlBulkCopy bulkCopy =
new SqlBulkCopy(sqlConnectionString))
{
bulkCopy.DestinationTableName = "UserDB";
bulkCopy.WriteToServer(dr);
return;
}
}
}
データベースサーバー全体がクラッシュしていると言っていますか? –
@ LasseV.Karlsenはい、 'リモートホストがリモートホストによって強制的にクローズされたため、サーバーを再起動して再度接続する必要があります。 – user1084683