excel导入sql

chengangmax 2011-06-19 01:06:42
SqlConnection con = new SqlConnection(x)

写到excel导入sql这里就是执行不了 估计是x这里的链接字符有问题 请教大家要怎么比较好呢
...全文
72 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
chengangmax 2011-06-19
  • 打赏
  • 举报
回复
public static readonly string ee =
"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Documents and Settings\\Administrator\\桌面\\11.xls;Extended Properties='Excel 8.0;HDR=NO;IMEX=1'";
chengangmax 2011-06-19
  • 打赏
  • 举报
回复
public bool aadd(message aa)
{
using (SqlConnection con = new SqlConnection(a.ee))
{
con.Open();
SqlCommand cmd = new SqlCommand("INSERT INTO [message](bianhao,box, boxname, username, date VALUES (@bianhao,@box,@boxname,@username,@date)", con);
cmd.Parameters.AddWithValue("@bianhao", aa.Bianhao);
cmd.Parameters.AddWithValue("@box", aa.Box);
cmd.Parameters.AddWithValue("@boxname", aa.Boxname);
cmd.Parameters.AddWithValue("@username", aa.Username);
cmd.Parameters.AddWithValue("@date", aa.Date);
cmd.ExecuteNonQuery();
return true;
chengangmax 2011-06-19
  • 打赏
  • 举报
回复
//导入excel
private void button3_Click(object sender, EventArgs e)
{
if (MessageBox.Show("导入将直接覆盖以前的数据", "警告", MessageBoxButtons.OKCancel) == DialogResult.OK)
{
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
if (openFileDialog1.OpenFile() != null)
{
if (Comm.EduceExcel(openFileDialog1.FileName))
{
try
{
for (int i = 0; i < Comm.Dt.Rows.Count; i++)
{
message ok=new message();
ok.Id = Comm.Dt.Rows[i].ItemArray[0].ToString();
ok.Bianhao = Comm.Dt.Rows[i].ItemArray[1].ToString();
ok.Box = Comm.Dt.Rows[i].ItemArray[2].ToString();
ok.Boxname = Comm.Dt.Rows[i].ItemArray[3].ToString();
ok.Username = Comm.Dt.Rows[i].ItemArray[4].ToString();
DateTime dt = new DateTime();
DateTime.TryParse(Comm.Dt.Rows[i].ItemArray[5].ToString(), out dt);
ok.Date = dt;
b okk = new b();
okk.aadd(ok);
}

getdate();
MessageBox.Show("恭喜,好友信息导入成功!\t\t", "提示");
}
catch
{
MessageBox.Show("对不起,好友信息导入失败!\t\t", "提示");
}
}
}
}
porschev 2011-06-19
  • 打赏
  • 举报
回复

错误帖上来。。。
chengangmax 2011-06-19
  • 打赏
  • 举报
回复
就是关于excel导入sql的
chengangmax 2011-06-19
  • 打赏
  • 举报
回复
Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\\Documents and Settings\\Administrator\\桌面\\11.xls;Extended Properties='Excel 8.0;HDR=NO;IMEX=1

就是这个啊 我用桌面的一个文件测试 还是不行
porschev 2011-06-19
  • 打赏
  • 举报
回复

SqlConnection con = new SqlConnection("server=.;uid=用户名;pwd=密码;database=数据库名");

你这问题和你题目都对不上。。
renyiqiu 2011-06-19
  • 打赏
  • 举报
回复
请问清楚点
porschev 2011-06-19
  • 打赏
  • 举报
回复

。。

我哪知道你X里写的什么啊

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧