|
本帖最后由 stormfly 于 2013-10-18 14:48 编辑
在grid的工具栏上,加了FileUpLoad控件,上传excel,然后批量导入数据库,数据成功导入库后;重新执行BindData,以便显示导入的数据,但,报脚本错误,然后导入的数据不显示,不知道是哪里的问题。
页面:
错误:
前台代码:
<x:Grid ID="Grid1" runat="server" BoxFlex="1" ShowBorder="true" ShowHeader="false"
EnableCheckBoxSelect="true" EnableRowNumber="true" EnableRowNumberPaging="true"
DataKeyNames="Id,CompanyName" AllowSorting="true" SortColumnIndex="0"
SortDirection="ASC" AllowPaging="true" IsDatabasePaging="true"
>
<Toolbars>
<x:Toolbar ID="Toolbar1" runat="server">
<Items>
<x:Button ID="btnDeleteSelected" Icon="Delete" runat="server" Text="删除选中记录">
</x:Button>
<x:ToolbarSeparator ID="ToolbarSeparator1" runat="server">
</x:ToolbarSeparator>
<x:ToolbarFill ID="ToolbarFill1" runat="server">
</x:ToolbarFill>
<x:Button ID="btnNew" runat="server" Icon="Add" EnablePostBack="false" Text="新增">
</x:Button>
<x:FileUpload ID="fuUserList" runat="server" Label="批量导入" ButtonText="批量导入" ButtonOnly="true" ButtonIcon="Anchor" AutoPostBack="true"></x:FileUpload>
</Items>
</x:Toolbar>
</Toolbars>
后台代码:
DataTable tb = this.getDataTableFromExcel(file);
int count = 0;
bool flag = false;
string msg = "";
string userlist = "";
using (TransactionScope scope = new TransactionScope())
{
foreach (DataRow item in tb.Rows)
{
string companyname = item[0].ToString();
string username = item[1].ToString();
string sex = item[2].ToString();
string age = item[3].ToString();
string userid = item[4].ToString();
string enterdate = item[5].ToString();
string worktypename = item[6].ToString();
string worktypeid = "";
string companyid = "";
//判断工种
XFieldValueCollection fv_lst = new Select().From<XFieldValue>().Where(XFieldValue.VTextColumn).IsEqualTo(worktypename).And(XFieldValue.VFKeyColumn).IsEqualTo("Trades").ExecuteAsCollection<XFieldValueCollection>();
if (fv_lst.Count <= 0)
{
msg = string.Format("姓名:{0}的工种{1}有误!请确保数据字典里有该工种!", username, worktypename);
flag = false;
break;
}
else
{
worktypeid = fv_lst[0].VCode;
}
//获取公司名称
XCompanyInfoCollection ci_lst = new Select().From<XCompanyInfo>().Where(XCompanyInfo.CompanyNameColumn).IsEqualTo(companyname).ExecuteAsCollection<XCompanyInfoCollection>();
if (ci_lst.Count <= 0)
{
msg = string.Format("姓名:{0}的单位名称{1}有误!请确保公司单位存在!", username, worktypename);
flag = false;
break;
}
else
{
companyid = ci_lst[0].Id.ToString();
}
//判断身份证是否已存在
if (new Select().From<XMedicalUserInfo>().Where(XMedicalUserInfo.UserIDCardColumn).IsEqualTo(userid).GetRecordCount() > 0)
{
userlist += username+",";
msg = userlist+"身份证已存在!";
continue;
}
XMedicalUserInfo userinfo = new XMedicalUserInfo();
userinfo.UserCnName = username;
userinfo.UserSex = sex;
userinfo.UserAge = Convert.ToInt32(age);
userinfo.CompanyId = ci_lst[0].Id;
userinfo.EnterDate = Convert.ToDateTime( enterdate);
userinfo.Trades = Convert.ToInt32(worktypeid);
userinfo.Status = 1;
userinfo.UserIDCard = userid;
userinfo.CreateDate = DateTime.Now;
userinfo.Save();
count++;
flag = true;
}
if (flag)
{
scope.Complete();
BindGrid();
Alert.Show(string.Format("数据批量导入成功!共导入{0}条记录!", count), "消息对话框", MessageBoxIcon.Information);
}
else
{
Alert.Show(string.Format("数据批量导入失败:{0}!",msg),MessageBoxIcon.Error);
}
}
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?立即注册
x
|