我上传了一个Excel文件,我将这些行分为2个表,一个包含有效和可接受的数据,另一个包含需要编辑的数据以便进行验证。 我有3列:名称,电子邮件和移动 我在查询具有有效移动值的行(数值,不允许使用字母)时遇到问题
(滚动下来直到你接到“我的问题是在下面查询” 这是我的代码:
protected void UploadBtn_Click(object sender, EventArgs e)
{
string filename = string.Empty;
if(FileUpload1.HasFile)
{
try
{
string[] allowFile = { ".xls", ".xlsx" };
string fileExt = System.IO.Path.GetExtension(FileUpload1.PostedFile.FileName);
bool isVaidFile = allowFile.Contains(fileExt);
if (!isVaidFile)
{
MessageLbl.ForeColor = System.Drawing.Color.Red;
MessageLbl.Text = "Must be an Excel file";
}
else
{
int fileSize = FileUpload1.PostedFile.ContentLength;
if (fileSize <= 1048576)
{
filename = Path.GetFileName(Server.MapPath(FileUpload1.FileName));
FileUpload1.PostedFile.SaveAs(Server.MapPath("~/UploadExcel/") + filename);
string filePath = Server.MapPath("~/UploadExcel/") + filename;
OleDbConnection con = null;
if (fileExt == ".xls")
{
con = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties=\"Excel 8.0;HDR=Yes;IMEX=1\";");
}
else if (fileExt == ".xlsx")
{
con = new OleDbConnection(@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + filePath + ";Extended Properties=\"Excel 12.0 Xml;HDR=YES;IMEX=1\";");
}
con.Open();
DataTable dt = con.GetOleDbSchemaTable(OleDbSchemaGuid.Tables, null);
string getExcelSheetName = dt.Rows[0]["Table_Name"].ToString();
OleDbCommand ExcelCommand =
new OleDbCommand(@" SELECT * FROM [" + getExcelSheetName + @"]", con);
OleDbCommand EditExcelCommand =
new OleDbCommand(@" SELECT * FROM [" + getExcelSheetName + @"] where Email not like '%@%.com' or Email is null or Name is null or Mobile is null or Mobile not like '^[[:digit:]]+$'", con);
//
//
// MY PROBLEM IS IN THE BELOW QUERY
//
//
OleDbCommand ValidExcelCommand =
new OleDbCommand(@"SELECT * FROM [" + getExcelSheetName + @"] where ((Email like '%@%.com') and (Name is not null) and (Mobile like '^[[:digit:]]+$'))", con);
OleDbDataAdapter EditAdapter = new OleDbDataAdapter(EditExcelCommand);
OleDbDataAdapter ValidAdapter = new OleDbDataAdapter(ValidExcelCommand);
OleDbDataAdapter ExcelAdapter = new OleDbDataAdapter(ExcelCommand);
DataSet ExcelDataSet = new DataSet();
DataSet EditDataSet = new DataSet();
DataSet ValidDataset = new DataSet();
EditAdapter.Fill(EditDataSet);
ValidAdapter.Fill(ValidDataset);
ExcelAdapter.Fill(ExcelDataSet);
con.Close();
GridView5.DataSource = ValidDataset;
GridView4.DataSource = EditDataSet;
GridView3.DataSource = ExcelDataSet;
GridView3.DataBind();
GridView4.DataBind();
GridView5.DataBind();
}
else
{
MessageLbl.Text = "Attachment file size should not be greater than 1 MB";
}
}
}
catch (Exception ex)
{
MessageLbl.Text = "Error occurred while uploading file. " + ex.Message;
}
}
else
{
MessageLbl.Text = "";
}
}
图片包含我正在获得的结果
我的问题是为什么我的ValidTable保持为空?我的查询语句有问题吗?
答案 0 :(得分:0)
您的代码看起来有效,我怀疑您的select语句可能存在问题。创建一个简单的字符串变量并将其设置为等于以下命令:
string validExcelCommandText = "SELECT * FROM [" + getExcelSheetName + @"] where ((Email like '%@%.com') and (Name is not null) and (Mobile like '^[[:digit:]]+$'))";
在此行上放置一个断点并运行您的应用程序,取值validExcelCommandText
并在SQL Server Management Studio中运行查询。如果它没有返回任何结果,那么修复您的SQL查询,如果它然后确保没有逻辑隐藏页面上的GridView
控件或检查后面代码中的绑定。
答案 1 :(得分:0)
我将查询语句重写为以下内容并且有效
string ValidExcelCommandText = @"SELECT * FROM [" + getExcelSheetName + @"] where ((Email like '%@_%.com') and (Name is not null) and not ( ISNUMERIC(Mobile) = 0))";