Chào mừng đến với Diễn đàn lập trình - Cộng đồng lập trình.
Kết quả 1 đến 2 của 2
  1. #1
    Ngày tham gia
    Sep 2015
    Bài viết
    0

    Xin mọi người xem giúp em code này với! Sao em chạy toàn lỗi à!

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;
    using System.Data.OleDb;

    namespace Lamlai
    {
    public partial class FormMain : Form
    {
    public FormMain()
    {
    InitializeComponent();
    }

    private void FormMain_Load(object sender, EventArgs e)
    {

    }

    private void btnBrowse_Click(object sender, EventArgs e)
    {
    OpenFileDialog ofd = new OpenFileDialog();
    txtFilePath.Text = ofd.ShowDialog() == DialogResult.OK ? ofd.FileName : "";
    }

    private void btnImport_Click(object sender, EventArgs e)
    {
    if (!CheckInput())
    return;
    DataTable data = ReadDataFromExcel();
    ImportIntoDatabase(data);
    ShowData();
    }
    private bool CheckInput()
    {
    if (txtFilePath.Text.Trim() == "")
    {
    MessageBox.Show("Xin chọn file");
    return false;
    }
    return true;
    }
    private DataTable ReadDataFromExcel()
    {
    string connectionString ="Provider=Microsoft.ACE.OLEDB.12.0;Data Source="+txtFilePath+";Extended Properties=\"Excel 12.0 Xml;HDR=YES\";";
    OleDbConnection oledbConn = new OleDbConnection();
    DataTable data = null;
    try
    {
    //Mở kết nối.
    oledbConn.Open();
    //Tạo đối tượng và query từ data với Sheet1
    OleDbCommand cmd = new OleDbCommand("SELECT * FROM [Sheet1$]", oledbConn);
    //Tạo đối tượng để thực thi query lấy data
    OleDbDataAdapter oleda = new OleDbDataAdapter();
    oleda.SelectCommand = cmd;
    //Tạo đối tượng để hứng dữ liệu
    DataSet ds = new DataSet();
    oleda.Fill(ds);
    data = ds.Tables[0];
    }
    catch (Exception ex)
    {
    MessageBox.Show(ex.ToString());
    }
    finally
    {
    oledbConn.Close();
    }
    return data;
    }
    private void ImportIntoDatabase(DataTable data)
    {
    if (data == null || data.Rows.Count == 0)
    {
    MessageBox.Show("Không có cơ sở dữ liệu");
    return;
    }
    HonghaTableAdapters.ButvietTableAdapter adapter = new HonghaTableAdapters.ButvietTableAdapter();
    string code = "", tenSP = "", mota = "";
    try
    {
    for (int i = 0; i < data.Rows.Count; i++)
    {
    code = data.Rows[i]["Code"].ToString().Trim();
    tenSP = data.Rows[i]["TenSP"].ToString().Trim();
    mota = data.Rows[i]["Mota"].ToString().Trim();
    Hongha.ButvietDataTable a = adapter.GetDataBy(code);
    if (a == null || a.Rows.Count == 0)
    {
    adapter.InsertButviet(code, tenSP, mota);
    }
    else
    {
    adapter.UpdateInfoByCode(tenSP, mota, code);
    }
    }
    }
    catch (Exception ex)
    {
    MessageBox.Show(ex.ToString());
    }
    MessageBox.Show("Kết thúc quá trình Import");
    }
    private void ShowData()
    {
    HonghaTableAdapters.ButvietTableAdapter adapter = new HonghaTableAdapters.ButvietTableAdapter();
    dvgButviet.DataSource = adapter.GetData();
    }
    }
    }
    Em cho debug thì chạy lần đầu, toàn báo lỗi ở đoạn mở kết nối, lần chạy thứ 2 thì đơ luôn máy! Mọi người giúp mình với!

  2. #2
    Trích dẫn Gửi bởi cattrangcodon
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;
    using System.Data.OleDb;

    namespace Lamlai
    {
    public partial class FormMain : Form
    {
    public FormMain()
    {
    InitializeComponent();
    }

    private void FormMain_Load(object sender, EventArgs e)
    {

    }

    private void btnBrowse_Click(object sender, EventArgs e)
    {
    OpenFileDialog ofd = new OpenFileDialog();
    txtFilePath.Text = ofd.ShowDialog() == DialogResult.OK ? ofd.FileName : "";
    }

    private void btnImport_Click(object sender, EventArgs e)
    {
    if (!CheckInput())
    return;
    DataTable data = ReadDataFromExcel();
    ImportIntoDatabase(data);
    ShowData();
    }
    private bool CheckInput()
    {
    if (txtFilePath.Text.Trim() == "")
    {
    MessageBox.Show("Xin chọn file");
    return false;
    }
    return true;
    }
    private DataTable ReadDataFromExcel()
    {
    string connectionString ="Provider=Microsoft.ACE.OLEDB.12.0;Data Source="+txtFilePath+";Extended Properties=\"Excel 12.0 Xml;HDR=YES\";";
    OleDbConnection oledbConn = new OleDbConnection();
    DataTable data = null;
    try
    {
    //Mở kết nối.
    oledbConn.Open();
    //Tạo đối tượng và query từ data với Sheet1
    OleDbCommand cmd = new OleDbCommand("SELECT * FROM [Sheet1$]", oledbConn);
    //Tạo đối tượng để thực thi query lấy data
    OleDbDataAdapter oleda = new OleDbDataAdapter();
    oleda.SelectCommand = cmd;
    //Tạo đối tượng để hứng dữ liệu
    DataSet ds = new DataSet();
    oleda.Fill(ds);
    data = ds.Tables[0];
    }
    catch (Exception ex)
    {
    MessageBox.Show(ex.ToString());
    }
    finally
    {
    oledbConn.Close();
    }
    return data;
    }
    private void ImportIntoDatabase(DataTable data)
    {
    if (data == null || data.Rows.Count == 0)
    {
    MessageBox.Show("Không có cơ sở dữ liệu");
    return;
    }
    HonghaTableAdapters.ButvietTableAdapter adapter = new HonghaTableAdapters.ButvietTableAdapter();
    string code = "", tenSP = "", mota = "";
    try
    {
    for (int i = 0; i < data.Rows.Count; i++)
    {
    code = data.Rows[i]["Code"].ToString().Trim();
    tenSP = data.Rows[i]["TenSP"].ToString().Trim();
    mota = data.Rows[i]["Mota"].ToString().Trim();
    Hongha.ButvietDataTable a = adapter.GetDataBy(code);
    if (a == null || a.Rows.Count == 0)
    {
    adapter.InsertButviet(code, tenSP, mota);
    }
    else
    {
    adapter.UpdateInfoByCode(tenSP, mota, code);
    }
    }
    }
    catch (Exception ex)
    {
    MessageBox.Show(ex.ToString());
    }
    MessageBox.Show("Kết thúc quá trình Import");
    }
    private void ShowData()
    {
    HonghaTableAdapters.ButvietTableAdapter adapter = new HonghaTableAdapters.ButvietTableAdapter();
    dvgButviet.DataSource = adapter.GetData();
    }
    }
    }
    Em cho debug thì chạy lần đầu, toàn báo lỗi ở đoạn mở kết nối, lần chạy thứ 2 thì đơ luôn máy! Mọi người giúp mình với!
    Bạn dùng OleDbConnection nhưng chưa truyền connectionString vào.
    Tutorial ở đây:
    http://msdn.microsoft.com/en-us/libr...(v=vs.71).aspx
    Bạn đọc kĩ Example của C# chỗ dùng OleDbConnection nhé, họ có truyền connectionString vào nữa. [IMG]images/smilies/smile.png[/IMG]

 

 

Quyền viết bài

  • Bạn Không thể gửi Chủ đề mới
  • Bạn Không thể Gửi trả lời
  • Bạn Không thể Gửi file đính kèm
  • Bạn Không thể Sửa bài viết của mình
  •