public bool SaveToDataBase(string filename)
{
List<List<object>> CitReport = ReaderCIT(filename);
CitReport.RemoveAt(0);
bool b = false;
SqlConnection con = new SqlConnection(AppConfiguration.CITConnectionString);
try
{
string sql = "insert into citreport values(";
foreach (List<object> item in CitReport)
{
for (int i = 0; i < item.Count; i++)
{
if (i == 0)
{
sql += "'" + Convert.ToString(item[i]) + "'";
}
else
{
sql += "," + "'" + Convert.ToString(item[i]) + "'";
}
}
break;
}
sql +=",'"+ DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "')";
SqlCommand com = new SqlCommand(sql, con);
con.Open();
int sum = com.ExecuteNonQuery();
com.Dispose();
if (sum > 0)
{
b = true;
}
}
catch (Exception ex)
{ throw ex;
}
finally { con.Close(); con.Dispose(); }
return b;
}这段代码是导入一个最少几百条的Exel表到数据到数据库
我不会事务之类的东西,求高手优化一下 感激感激.....
解决方案 »
- ActiveX控件一直重复安装
- 关于多线程互斥代码区的问题.....急啊................
- 如何自动取datagrid中的值
- 在ASP.NET中用C#语言编写事物提醒~~~~~~~~[在线等]
- RichTextBox的内容传到另一个窗体,如何传,要含字体格式?
- 应输入类型 命名空间或文件尾
- 各位大大来看看这个关于sendkey.send用法的很菜的问题!
- 求助~C#中picturebox控件读取数据库路径并显示出来
- 救命,remoting开发的系统编码完成却发现一个关于异常的严重问题
- 急,上午就要解决,在线等~~~~~~~~解决马上结贴
- remoting中只能在客户端读取服务器端数据 ,却不能进行方法调用
- c#获取摄像头的ID问题!
2、这段代码的逻辑应该全是错的吧,SQL拼出来是什么样的您有输出来看过吗?
3、先要从0到1,再从1到100000。 即:先把结果弄出来,再考虑优化的事情有人回复了我的帖子,竟然没发现。用《Csdn收音机》告别烦恼!
读取EXCEL到dataset,sqldataadapter更新数据库
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + filePath + ";Extended Properties='Excel 8.0;HDR=False;IMEX=1'";
using(OleDbConnection OleConn = new OleDbConnection(strConn))
{
OleConn.Open();
String sql = "SELECT * FROM [Sheet1$]";
OleDbDataAdapter OleDaExcel = new OleDbDataAdapter(sql, OleConn);
DataSet ds= new DataSet();
OleDaExcel.Fill(ds);
OleConn.Close();
}
foreach 下面加break 是让他直插入一条数据
如过不加就会所有的数据都在一个insert里面 这我知道
public bool SaveCITToDataBase(string filename)
{
List<List<object>> CitReport = ReaderCIT(filename);
CitReport.RemoveAt(0);
bool b = false;
SqlConnection con = new SqlConnection(AppConfiguration.CITConnectionString);
try
{
string sql = string.Empty;
foreach (List<object> item in CitReport)
{
sql += "insert into citreport values(";
for (int i = 0; i < item.Count; i++)
{
if (i == 0)
{
sql += "'" + Convert.ToString(item[i]) + "'";
}
else
{
sql += "," + "'" + Convert.ToString(item[i]) + "'";
}
}
sql +=",'"+ DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "')";
}
SqlCommand com = new SqlCommand(sql, con);
con.Open();
int sum = com.ExecuteNonQuery();
com.Dispose();
if (sum > 0)
{
b = true;
}
}
catch (Exception ex)
{ throw ex;
}
finally { con.Close(); con.Dispose(); }
return b;
}