当前位置:
首页 > 编程开发 > 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();
}
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();
}
最新更新
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
Python初学者友好丨详解参数传递类型
如何有效管理爬虫流量?
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
SQL Server -- 解决存储过程传入参数作为s
JavaScript判断两个数组相等的四类方法
js如何操作video标签
React实战--利用甘特图和看板,强化Paas平
【记录】正则替换的偏方
前端下载 Blob 类型整理
抽象语法树AST必知必会
关于JS定时器的整理
JS中使用Promise.all控制所有的异步请求都完
js中字符串的方法
import-local执行流程与node模块路径解析流程