如何从数据表中删除空行
我正在努力将数据从 Excel 工作表导入到数据库。 Excel 工作表包含一些空行,我想删除这些空行,然后将清除的数据插入数据库。
我通过引用其他代码编写了一个代码,这是用于插入值的代码:
OleDbConnection cnn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" + txtExcelFile.Text + "';Extended Properties= 'Excel 8.0;HDR=Yes;IMEX=1'");
//DataTable dt = new DataTable();
try
{
cnn.Open();
OleDbDataAdapter data = new OleDbDataAdapter("select * from [Customers$]", cnn);
data.Fill(dsExcel);
dgvCustomers.ColumnHeadersVisible = false;
SqlConnection connection = new SqlConnection("Data Source=COMPUTER-8EB749;Initial Catalog=KITS;Integrated Security=true");
connection.Open();
for (int i = 0; i < dsExcel.Tables[0].Rows.Count; i++)
{
string ID = ds.Tables[0].Rows[i][0].ToString();
Int16 CustID = Convert.ToInt16(ID);
string CustName = dsExcel.Tables[0].Rows[i][1].ToString();
string CardScheme = dsExcel.Tables[0].Rows[i][2].ToString();
string Outlet = dsExcel.Tables[0].Rows[i][3].ToString();
string TerminalNum = dsExcel.Tables[0].Rows[i][4].ToString();
Int32 Terminal = Convert.ToInt32(TerminalNum);
string Date1 = dsExcel.Tables[0].Rows[i][5].ToString();
DateTime Date = Convert.ToDateTime(Date1);
string Time = dsExcel.Tables[0].Rows[i][6].ToString();
DateTime DateTime = Convert.ToDateTime(Time);
string Amount1 = ds.Tables[0].Rows[i][7].ToString();
double Amount = Convert.ToDouble(Amount1);
SqlCommand com = new SqlCommand("insert into Customer(CustID,CustName,CardScheme,Outlet,TerminalNum,TranDate,TranDateTime,Amount) values ('" + CustID + "','" + CustName + "','" + CardScheme + "','" + Outlet + "','" + Terminal + "','" + Date + "','" + DateTime + "','" + Amount + "')", connection);
com.ExecuteNonQuery();
}
connection.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
MessageBox.Show("Data Inserted Successfully.");
}
谁能告诉我如何删除空行以便我只能插入数据?!
I am working on importing data from an Excel sheet to database. The Excel sheet contains few empty rows and I want to remove those empty rows, then insert cleared data into database.
I have written a code by referring other code, this is the code for inserting values:
OleDbConnection cnn = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source='" + txtExcelFile.Text + "';Extended Properties= 'Excel 8.0;HDR=Yes;IMEX=1'");
//DataTable dt = new DataTable();
try
{
cnn.Open();
OleDbDataAdapter data = new OleDbDataAdapter("select * from [Customers$]", cnn);
data.Fill(dsExcel);
dgvCustomers.ColumnHeadersVisible = false;
SqlConnection connection = new SqlConnection("Data Source=COMPUTER-8EB749;Initial Catalog=KITS;Integrated Security=true");
connection.Open();
for (int i = 0; i < dsExcel.Tables[0].Rows.Count; i++)
{
string ID = ds.Tables[0].Rows[i][0].ToString();
Int16 CustID = Convert.ToInt16(ID);
string CustName = dsExcel.Tables[0].Rows[i][1].ToString();
string CardScheme = dsExcel.Tables[0].Rows[i][2].ToString();
string Outlet = dsExcel.Tables[0].Rows[i][3].ToString();
string TerminalNum = dsExcel.Tables[0].Rows[i][4].ToString();
Int32 Terminal = Convert.ToInt32(TerminalNum);
string Date1 = dsExcel.Tables[0].Rows[i][5].ToString();
DateTime Date = Convert.ToDateTime(Date1);
string Time = dsExcel.Tables[0].Rows[i][6].ToString();
DateTime DateTime = Convert.ToDateTime(Time);
string Amount1 = ds.Tables[0].Rows[i][7].ToString();
double Amount = Convert.ToDouble(Amount1);
SqlCommand com = new SqlCommand("insert into Customer(CustID,CustName,CardScheme,Outlet,TerminalNum,TranDate,TranDateTime,Amount) values ('" + CustID + "','" + CustName + "','" + CardScheme + "','" + Outlet + "','" + Terminal + "','" + Date + "','" + DateTime + "','" + Amount + "')", connection);
com.ExecuteNonQuery();
}
connection.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
MessageBox.Show("Data Inserted Successfully.");
}
Can anyone say me how can I remove empty rows so that i can insert only data?!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(16)
这将删除每列不包含任何内容或包含空格的所有行:
This will remove all rows that which each of it's columns contain either nothing or white space:
试试这个。
请注意,我只是检查 ID 是否为空,并且不会插入任何此类行,因为 ID 将成为表中的 PK。
Try this.
Please note that I am just checking if ID is null and not inserting any such rows as ID will be the PK in your table.
这将从数据表中删除所有空行:
或
This will remove all empty rows from the data table:
OR
在这里,如果任何行中有空白条目,我也会跳过这些行。
Here I m also skipping the rows if they have blank entry in any of the row.
我已经制作了这个可以解决问题的私有方法。
它接受 DataTable 作为参数并返回不带空行的相同 DataTable。
I've made this private method that does the trick.
It takes a DataTable as argument and returns the same DataTable without empty rows.
检查空行
To check Empty Rows
为什么不在插入空行之前直接忽略它们呢?
像这样:
Why not simply ignore empty rows directly before you are inserting them?
Like this:
我修改了Cfrim的答案。您需要检查空字符串和空白字符串。空白来自于删除的单元格,空白来自于删除的数据。
I modified Cfrim's answer. You need to check for both empty and whitespace strings. The white space comes from the deleted cells and the empty space comes from deleted data.
你的数据库本身有空行?那很奇怪。当您执行选择查询时,可以通过说主键列不为 NULL 来过滤它
Your DB itself has empty rows?? Thats quite strange. May be filter it while you do a select query by saying a primary key column is not NULL
这对我来说很完美:
this works perfect for me:
我在 @Levitikon 帖子 https://stackoverflow.com/a/9233696/5848472 中做了一些更改
使用 @shA.t comment ,此代码删除数据表中的所有空行和列:
I change a little in @Levitikon post https://stackoverflow.com/a/9233696/5848472
with @shA.t comment , and this code remove all empty Rows and Columns in datatable:
这对我有用。如果我们不检查行并直接执行
CopyToDataTable()
,那么当数据表有空行时,您可能会收到异常。This worked for me. If we do not check rows and directly do
CopyToDataTable()
then you may get an exception when the data table has empty rows.根据现有的答案,我使用以下
用法然后
Based on existing answers I use following
Usage is then
要从数据表中删除空行:
To remove Empty Rows from DataTable: