请教将Excel数据表导入DataGrid并存入Oracle的方法(数据表已建好)附程序(200分)

D

dxssxd

Unregistered / Unconfirmed
GUEST, unregistred user!
这是我写的程序 (不带数据库操作部分)
不知道什么原因编译不过去,请大家帮我看看 方便的话请教一下代码
using System ;
using System.Drawing ;
using System.Collections ;
using System.ComponentModel ;
using System.Windows.Forms ;
using System.Data ;
using System.Data.OleDb ;
namespace 读Excel到DataGrid
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.DataGrid dataGrid1;
private System.Windows.Forms.Button button1;
private System.Data.DataSet myDataSet ;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.TextBox textBox1;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
GetConnect ( ) ;

//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if(components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
[STAThread]
static void Main()
{
Form1 mainfrm = new Form1();
Application.Run(mainfrm);
}
private void GetConnect ( )
{
//创建一个数据链接
string strCon = " Provider = Microsoft.Jet.OLEDB.4.0 ;
Data Source = '"+this.textBox1.Text+"';Extended Properties=Excel 8.0" ;
OleDbConnection myConn = new OleDbConnection ( strCon ) ;
string strCom = " SELECT * FROM [Sheet1$] " ;
myConn.Open ( ) ;
//打开数据链接,得到一个数据集
OleDbDataAdapter myCommand = new OleDbDataAdapter ( strCom , myConn ) ;
//创建一个 DataSet对象
myDataSet = new DataSet ( ) ;
//得到自己的DataSet对象
myCommand.Fill ( myDataSet , "[Sheet1$]" ) ;
//关闭此数据链接
myConn.Close ( ) ;
}

#region Windows Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.dataGrid1 = new System.Windows.Forms.DataGrid();
this.button1 = new System.Windows.Forms.Button();
this.button2 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).begin
Init();
this.SuspendLayout();
//
// dataGrid1
//
this.dataGrid1.DataMember = "";
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(16, 120);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.Size = new System.Drawing.Size(632, 296);
this.dataGrid1.TabIndex = 0;
//
// button1
//
this.button1.Location = new System.Drawing.Point(264, 64);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(112, 24);
this.button1.TabIndex = 1;
this.button1.Text = "倒入数据";
//
// button2
//
this.button2.Location = new System.Drawing.Point(264, 24);
this.button2.Name = "button2";
this.button2.Size = new System.Drawing.Size(112, 24);
this.button2.TabIndex = 2;
this.button2.Text = "浏览";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(104, 32);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(136, 21);
this.textBox1.TabIndex = 3;
this.textBox1.Text = "textBox1";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(664, 430);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.textBox1,
this.button2,
this.button1,
this.dataGrid1});
this.Name = "Form1";
this.Text = "Form1";
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
this.ResumeLayout(false);
}
#endregion
private void button2_Click(object sender, System.EventArgs e)
{
OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = "C://" ;
openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*" ;
openFileDialog1.FilterIndex = 2 ;
openFileDialog1.RestoreDirectory = true ;
if(openFileDialog1.ShowDialog() == DialogResult.OK)
{
this.textBox1.Text = openFileDialog1.FileName.ToString();
}
}
}
}
 
我编译没有任何问题,你有出错信息吗?
你的工程中该引用的dll,是不是都已引用了?
另外,我还有两个建议:
1。namespace 读Excel到DataGrid
你起一个这样的名字,以后引用起来,够你麻烦的:)
2。 public Form1()
{
InitializeComponent();
GetConnect ( ) ;
}
这是Form这个类的Constructor,GetConnect不属于构建部分,
你还是把他拿走,放到Form_Load里去吧。
 
Delphi我作过
 
上面作法會有問題,如EXCEL.XLS你有一欄間有 日期型資料 又有其他型別,就有問題.
還是用mircosoft com元件去實現.
 
顶部