VB.net 2010 视频教程 VB.net 2010 视频教程 python基础视频教程
SQL Server 2008 视频教程 c#入门经典教程 Visual Basic从门到精通视频教程
当前位置:
首页 > 编程开发 > Objective-C编程 >
  • C#将Excel作数据源

C#将Excel作数据源(需在项目上“添加引用”—> “Microsoft Office Excel”)
private void btn_Import_Click(object sender, EventArgs e)
{
 
    string strConn;
 
    // IMEX=1 可把混合型作为文本型读取,避免null值
 
    strConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=Test.xls;Extended Properties='Excel 8.0;HDR=False;IMEX=1'";
 
    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, "MyTable");//MyTable是自定义的别名
 
    dgv.DataSource = ds;
 
    dgv.DataMember = "MyTable";
 
    OleConn.Close();
 
}

 
实战练习一:C# 登录模块示例代码

登录窗体详细设置(在InitializeComponent();处击右键,选择“转到定义”获取的代码)
private void InitializeComponent()
        {
            this.label1 = new System.Windows.Forms.Label();
            this.label2 = new System.Windows.Forms.Label();
            this.txt_User = new System.Windows.Forms.TextBox();
            this.txt_Pwd = new System.Windows.Forms.TextBox();
            this.btn_Login = new System.Windows.Forms.Button();
            this.btn_Exit = new System.Windows.Forms.Button();
            this.SuspendLayout();
            //
            // label1
            //
            this.label1.AutoSize = true;
            this.label1.Location = new System.Drawing.Point(35, 36);
            this.label1.Name = "label1";
            this.label1.Size = new System.Drawing.Size(41, 12);
            this.label1.TabIndex = 0;
            this.label1.Text = "工号:";
            //
            // label2
            //
            this.label2.AutoSize = true;
            this.label2.Location = new System.Drawing.Point(35, 90);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(41, 12);
            this.label2.TabIndex = 1;
            this.label2.Text = "密码:";
            //
            // txt_User
            //
            this.txt_User.Location = new System.Drawing.Point(91, 33);
            this.txt_User.Name = "txt_User";
            this.txt_User.Size = new System.Drawing.Size(100, 21);
            this.txt_User.TabIndex = 2;
            //
            // txt_Pwd
            //
            this.txt_Pwd.Location = new System.Drawing.Point(91, 87);
            this.txt_Pwd.Name = "txt_Pwd";
            this.txt_Pwd.PasswordChar = '*';
            this.txt_Pwd.Size = new System.Drawing.Size(100, 21);
            this.txt_Pwd.TabIndex = 3;
            //
            // btn_Login
            //
            this.btn_Login.Location = new System.Drawing.Point(37, 133);
            this.btn_Login.Name = "btn_Login";
            this.btn_Login.Size = new System.Drawing.Size(61, 23);
            this.btn_Login.TabIndex = 4;
            this.btn_Login.Text = "登录";
            this.btn_Login.UseVisualStyleBackColor = true;
            this.btn_Login.Click += new System.EventHandler(this.btn_Login_Click);
            //
            // btn_Exit
            //
            this.btn_Exit.Location = new System.Drawing.Point(137, 133);
            this.btn_Exit.Name = "btn_Exit";
            this.btn_Exit.Size = new System.Drawing.Size(54, 23);
            this.btn_Exit.TabIndex = 5;
            this.btn_Exit.Text = "退出";
            this.btn_Exit.UseVisualStyleBackColor = true;
            this.btn_Exit.Click += new System.EventHandler(this.btn_Exit_Click);
            //
            // Form1
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.ClientSize = new System.Drawing.Size(237, 186);
            this.Controls.Add(this.btn_Exit);
            this.Controls.Add(this.btn_Login);
            this.Controls.Add(this.txt_Pwd);
            this.Controls.Add(this.txt_User);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.label1);
            this.Name = "Form1";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "系统登录";
            this.Load += new System.EventHandler(this.Form1_Load);
            this.ResumeLayout(false);
            this.PerformLayout();
 
        }
 
登录按钮源码:
private void btn_Login_Click(object sender, EventArgs e)
        {
 
            //检查工号是否为空
            if (txt_User.Text == "")
            {
                MessageBox.Show("请先输入工号", "系统提示");
                txt_User.Focus();
                return;
            }
            else if (txt_Pwd.Text == "")
            {
                MessageBox.Show("请输入密码", "系统提示");
                txt_Pwd.Focus();
                return;
            }
 
            //定义连接字符串              
            string strConnection = "Server=168.10.10.86;initial catalog=Tele_Sale;user id=sa;password=sd;Connect Timeout=30";
 
            //用连接字符串创建一个数据库连接对象
            SqlConnection objConnection = new SqlConnection(strConnection);
 
            //使用数据库连接对象连接数据库
            objConnection.Open();
 
            //基于数据库连接对象创建一个sql命令
            SqlCommand SqlCmd = new SqlCommand("select * from MissInfo where MissID='"+txt_User.Text+"'", objConnection);
 
            //创建一个数据阅读器并从sql命令中读出数据
            SqlDataReader DataReader = SqlCmd.ExecuteReader();
 
     
            //使用数据阅读器中的字段内容
            if (DataReader.Read())
            {
                //读取密码
                if (DataReader[2].ToString() != txt_Pwd.Text)
                {
                    MessageBox.Show("对不起,您输入的工号或密码错误!", "系统提示");
                }
                else
                {
                    //MessageBox.Show("欢迎使用营销管理系统!", "系统提示");
                    //打开主程序界面
                    this.Hide();
                    Form2 f2 = new Form2(txt_User.Text);
                    f2.ShowDialog();
                    this.Close();                   
                }
            }
            else
            {
                MessageBox.Show("该工号不存在", "系统提示");
            }
 
            //关闭数据库连接及数据阅读器          
            objConnection.Close();
            DataReader.Close();
        }
 退出”按钮源码:
        private void btn_Exit_Click(object sender, EventArgs e)
        {
            this.Close();
        }

 


相关教程