
private Boolean dbUpdate()
{
string strSql = "select ID as 学号,name as 姓名, class as 班级 from tbl_Student"
//新建一个用于将dgvStudent数据 *** 作更新到数据库的内存表
DataTable dtUpdate= new DataTable()
//新建一个用于更新dgvStudent数据 *** 作的内存表
//利用da初始化dtUpdate的表结构(和数据)
SqlDataAdapter da = new SqlDataAdapter(strSql, sqlCon)
da.Fill(dtUpdate)
//初始化的数据需清除,以存放更新后的dgvStudent数据
dtUpdate.Rows.Clear()
//再建一个内存表,用于将更新后的dgvStudent数据逐条读取出来存入更新内存表中
DataTable dtShow = new DataTable()
//逐条读取出来存入更新内存表中
dtShow = (DataTable) dgvStudent.DataSource
for (int i = 0i <dtShow.Rows.Counti++)
{
dtUpdate.ImportRow(dtShow.Rows[i])
}
try
{
this.sqlCon.Open()
//使对DataSet所做的更改与关联的SQL Server数据库相协调
SqlCommandBuilder sqlCbld
sqlCbld = new SqlCommandBuilder(da)
//通过该da将更新后的dgvStudent数据(即已复制的dtUpdate)更新到数据库
da.Update(dtUpdate)
sqlCon.Close()
}
catch(Exception ex)
{
MessageBox.Show("数据库 *** 作失败:" + ex.Message.ToString(), "提示", MessageBoxButtons.OK, MessageBoxIcon.Exclamation)
return false
}
dtUpdate.AcceptChanges()
return true
}
首先excel表怎样导入数据库跟架构扯不上什么关系,你只要知道怎么写sql语句 *** 作即可给个十分简单的例子
protected void Button1_Click(object sender, EventArgs e)
{
//EXCEL路径
string path = FileUpload1.PostedFile.FileName
//把EXCEL读到DataTable 的方法
DataTable dt = ExcelToTable(path)
int count = 0
//下面就是读出所有的行,自己选择哪几项插入SQL就行了!
for (int i = 0i <dt.Rows.Counti++)
{
string jinshouren = dt.Rows[i][1].ToString()
string company = dt.Rows[i][2].ToString()
string people = dt.Rows[i][3].ToString()
string duty = dt.Rows[i][4].ToString()
string tel = dt.Rows[i][5].ToString()
string molible = dt.Rows[i][6].ToString()
string fanwei = dt.Rows[i][7].ToString()
string add = dt.Rows[i][8].ToString()
string QQ = dt.Rows[i][9].ToString()
string email = dt.Rows[i][10].ToString()
string laiyuan = dt.Rows[i][11].ToString()
string qingkuang = dt.Rows[i][12].ToString()
string test = dt.Rows[i][13].ToString()
string luxian = dt.Rows[i][14].ToString()
string jilu = dt.Rows[i][15].ToString()
string sql_select = string.Format("select count(*) from customer where Customername='{0}' and Customercompany='{1}' and Customermoblie='{2}'", people, company, tel)
string num = help.ExcuteDataSet(sql_select, CommandType.Text).Tables[0].Rows[0][0].ToString()
if (num != "1")
{
string sql = string.Format(@"insert into customer (Customername,Customersex,Customerduty,Customercompany,Customerbusiness,Customermoblie,Customerfax,Customertel,Customeraddress,CustomerQQ,CustomerEmail,roadline,Customernotes,teamwork,tracking)
values ('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}','{13}','{14}')", people, jinshouren, duty, company, fanwei, tel, laiyuan, molible, add, QQ, email, luxian, test, qingkuang, jilu)
count = help.ExcuteNoneQuery(sql, CommandType.Text)
}
}
if (count >0)
{
ShowMsg("导入成功!")
}
}
public static DataTable ExcelToTable(string Path)
{
string strConn = "Provider=Microsoft.Jet.OLEDB.4.0" +
"Data Source='" + Path + "'" + "Extended Properties='Excel 8.0HDR=YesIMEX=1'"
OleDbConnection conn = new OleDbConnection(strConn)
conn.Open()
string strExcel = ""
OleDbDataAdapter myCommand = null
DataSet ds = null
strExcel = "select * from [sheet1$]"//EXCEL表名(不是文件名)
myCommand = new OleDbDataAdapter(strExcel, strConn)
ds = new DataSet()
myCommand.Fill(ds, "table1")
conn.Dispose()
return ds.Tables[0]
}
visual studio的文本用三层架构导入数据库方法如下:三层架构中向表中添加数据的做法一般为,在表示层获得要添加的对象(即你输入的数据),然后调用业务层的添加方法。看了你写的代码,我给你的建议是:在你的GridView_RowCommand事件处理代码中,得到当前需要添加的数据,然后封装为一个对象,接着调用业务层的添加方法
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)