c# 테이블 정보를 가져와 데이터베이스에 삽입
1492 단어 VS
#region excel DataSet
public static DataSet ExcelToDataSet(string filename) // excel DataSet
{
string strCon = "Provider=Microsoft.Jet.OLEDB.4.0;" + "Extended Properties=Excel 8.0;" + "data source=" + filename;
OleDbConnection myConn = new OleDbConnection(strCon);
string strCom = " SELECT * FROM [Sheet1$]"; //"Sheet1"
myConn.Open();
OleDbDataAdapter myCommand = new OleDbDataAdapter(strCom, myConn);
DataSet ds;
ds = new DataSet();
myCommand.Fill(ds);
myConn.Close();
return ds;
}
#endregion
2. 지정된 표의 정보를 데이터베이스에 삽입
#region
using (var db = SugarHelper.GetInstance())
{
DataSet ds = ExcelToDataSet("E:\\111.xls"); //
for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
{
string goods_no = ds.Tables[0].Rows[i][" "].ToString();
int num = ds.Tables[0].Rows[i][" "].ObjToInt();
string sql = "INSERT INTO `TEST` (`goods_no`, `num`) VALUES ( '" + goods_no + "', '" + num + "')";
db.ExecuteCommand(sql);
}
}
#endregion