All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
asp.net2.0验证控件之ValidationSummary控件使用方法详
AJAX、AJAX实例及AJAX源代码(asp)_[AJAX教程]
Asp.Net页面输出到EXCEL_[Asp.Net教程]
用gridview显示来自excel表格里的数据_[Asp.Net教程]
asp.net2.0服务器控件之ImageButton控件
Login控件:用户登录失败的消息提示_.net资料_编程技术
C#教程:C#数据类型之值的类型
PHP能得到你是从什么页面过来的,referer的用处_php资料_编程技术
C#网络应用编程基础练习题与答案(五)_[Asp.Net教程]
如何让你的SQL运行得更快(2)_[SQL,Server教程]
Reading number is top 10 pictures
王艳写真温柔如水1
南昌铁路局宜春车务段攸县车站铁路职工福利房被开发商侵占
Sora aoi calligraphy show
毕姥爷事件,告诉你6条真理
A man's favorite things5
Forced sex girl living abroad2
采访谢楠
Ashlynn Brooke show proud chest measurement2
这才叫绝色美女1
A man's favorite things8
Download software ranking
C#编程思想
Unix video tutorial17
Boxer's Top ten classic battle8
Sora aoi's film--cangkong_Blue.Sky
Sora aoi, the nurse, uniform ,nursing assistant
塘西风月痕
Boxer Classic video3
Sora aoi 120 minutes
Unix video tutorial8
Take off clothes to survival
delv published in(发表于) 2013/12/30 4:33:14 Edit(编辑)
Excel中数据导入到SQL,Server数据库_mssql学习_编程技术

Excel中数据导入到SQL,Server数据库_mssql学习_编程技术

Excel中数据导入到SQL Server数据库_mssql学习_编程技术-你的首页-uuhomepage.com
using System.Data.SqlClient;
using System.Data.OleDb;
private void tsmiImportTeacherInfo_Click(object sender, EventArgs e)
{
DataSet ds;
if (ofdImport.ShowDialog()==DialogResult.OK)
{
ds = ImportExcel(this.ofdImport.FileName); //获得Excel
}
else
{
return;
}
int odr = 0;
SqlConnection cn = db.sqlconnection();
try
{
cn.Open();
string str = "insert into Teacher (TeacherId,Pwd,eachCourse,TeacherName) values(@TeacherId,@Pwd,@TeachCourse,@TeacherName)";
SqlCommand cmd = db.sqlcommand(str,cn);
int dsLength = ds.Tables[0].Rows.Count; //获得Excel中数据长度
for (int i = 0; i < dsLength; i++)
{
cmd.Parameters.Add("@TeacherId", SqlDbType.VarChar, 50).Value = ds.Tables["Teacher "].Rows[i][0];
cmd.Parameters.Add("@Pwd", SqlDbType.VarChar, 50).Value = ds.Tables["Teacher "].Rows[i][1];
cmd.Parameters.Add("@TeachCourse", SqlDbType.VarChar, 300).Value = ds.Tables["Teacher "].Rows[i][2];
cmd.Parameters.Add("@TeacherName", SqlDbType.VarChar, 50).Value = ds.Tables["Teacher "].Rows[i][3];
odr = cmd.ExecuteNonQuery();//提交
cmd.Parameters.Clear(); //清除前的赋值
}
//如果查到了数据,才使控制分页按钮生效
if (odr > 0)
{
MessageBox.Show("导入成功!", "系统提示", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
cn.Close();
}
catch (Exception ex)
{
throw ex;
}
}
public static DataSet ImportExcel(string file)
{
FileInfo FileInfo = new FileInfo(file);
if (!FileInfo.Exists)
{
return null;
}
string strcn = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + file + ";Extended Properties='Excel 12.0 Xml;HDR=YES;IMEX=1;'";
OleDbConnection cn = new OleDbConnection(strcn);
DataSet ds = new DataSet();
try
{
cn.Open();
string strcmd = "select * from [Sheet1$]";
OleDbDataAdapter da = new OleDbDataAdapter(strcmd,cn);
da.Fill(ds, "Teacher ");
return ds;
}
catch (Exception ex)
{
throw ex;
}
}
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/crx05/archive/2009/12/17/5023589.aspx




添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.