如何利用三层架构把数据写入数据库

如何利用三层架构把数据写入数据库,第1张

三层架构中向表中添加数据的做法一般为,在表示层获得要添加的对象(即你输入的数据),然后调用业务层的添加方法。看了你写的代码,我给你的建议是:在你的GridView_RowCommand事件处理代码中,得到当前需要添加的数据,然后封装为一个对象,接着调用业务层的添加方法,希望你能明白。

首先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]

}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/9986420.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-04
下一篇 2023-05-04

发表评论

登录后才能评论

评论列表(0条)

保存