程序可以运行,功能是把excel中的数据导入到服务器sqlser,为什么会报错无法转换成double格式?
我在sqlser中的数据格式试过char,vchar,nvchar等等,都是报这个错,程序中并没有转换格式啊。
procedure TForm1.Button1Click(Sender: TObject);
var ExcelApp, WorkBook,ExcelRowCount:Variant;
var i:integer;
begin
try
ExcelApp:=CreateOleObject('Excel.Application');
WorkBook := ExcelApp.WorkBooks.Open(edit1.Text);
ExcelApp.Visible := false;//设置excel是否可见
ExcelRowCount := WorkBook.WorkSheets[1].UsedRange.Rows.Count;
for i := 1 to excelrowcount + 1 do
begin
if ((excelapp.Cells[i,1].Value = '') and (ExcelApp.Cells[i,2].Value = '')) then exit
else
with ADOQuery1 do
begin
sql.add('insert into Table_1 (bs,hb) values (:bs,:hb)');
ADOQuery1.parameters.parambyname('bs').value:=(ExcelApp.Cells[i,1].Value);//断点显示在这里出错。/
ADOQuery1.parameters.parambyname('hb').value:=(ExcelApp.Cells[i,2].Value);
end;
end;
finally
WorkBook.Close;
ExcelApp.Quit;
ExcelApp := Unassigned;
WorkBook := Unassigned;
end;
end;
end.
回答:
如果 Variant 的值为 NULL 或空串,显然不能转换为 Double ,赋值语句左边类型是 Double ,右边类型是存储的 string 的 Variant ,需要在赋值之前进行验证。