ASP.NETR把Excel导入SQL数据库的自定义类
以下是引用片段: public class SaveToSql { public string strNewCodeId = string.Empty; public string strNewCode = string.Empty; public string strNewDes = string.Empty; public string strOldCodeId = string.Empty; public string strOldCode = string.Empty; public string strOldDes = string.Empty; public string Server = string.Empty; public string DataBase = string.Empty; public string Uid = string.Empty; public string Pwd = string.Empty ; private string TableName = string.Empty; /// <summary> /// 目的表的名称 /// </summary> public string TableNames { get{return TableName;} set{TableName=value;} } public SaveToSql() { } /// <summary> /// 保存数据 /// </summary> public void Save() { string strConPath = string.Format("server={0};database={1};uid={2};pwd={3}",this.Server,this.DataBase,this.Uid,this.Pwd); SqlConnection con1 = new SqlConnection(strConPath); con1.Open(); SqlCommand com1 = new SqlCommand(); com1.CommandText = "Insert into "+TableName+"(newCodeId,newCode,newDes,oldCodeId,oldCode,oldDes) Values(’"+strNewCodeId+"’,’"+strNewCode+"’,’"+strNewDes+"’,’"+strOldCodeId+"’,’"+strOldCode+"’,’"+strOldDes+"’)"; com1.Connection=con1; com1.ExecuteNonQuery(); con1.Dispose(); } } |