此代码从串行端口发送数据并保存在Excel中.如何更改此代码以保存在sql表中? [英] This code, send data from Serial Port and save in Excel. How to change this code to save in sql table?

查看:81
本文介绍了此代码从串行端口发送数据并保存在Excel中.如何更改此代码以保存在sql表中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
       Data = SerialPort1.ReadExisting
       If Data <> "" Then
           temp = Data.Length
           If counter = 1 Then

               ExcelApp = New Excel.Application
               ExcelBook = ExcelApp.Workbooks.Add(MisValue)
               ExcelSheet = ExcelBook.Sheets("sheet1")



               ExcelSheet.Cells(1, 1) = "No"
               ExcelSheet.Cells(1, 2) = "Date"
               ExcelSheet.Cells(1, 3) = "Time"
               ExcelSheet.Cells(1, 4) = "Idnumber"
               ExcelSheet.Cells(1, 5) = "NameIn"
               ExcelSheet.Cells(1, 6) = "StatusOut"
               FileName = "RekodKeluarMasuk"
               ExcelBook.SaveAs(TbFileLoc.Text & FileName & ".xls")

           End If

           ExcelSheet.Cells(counter + 1, 1) = counter
           ExcelSheet.Cells(counter + 1, 2) = DateString
           ExcelSheet.Cells(counter + 1, 3) = TimeString
           If Data = "OUT" Then
               ExcelSheet.Cells(counter + 1, 6) = Data
           Else
               ExcelSheet.Cells(counter + 1, 5) = "=" & Chr(34) & Data.Substring(11, temp - 11) & Chr(34) 
               ExcelSheet.Cells(counter + 1, 4) = "=" & Chr(34) & Data.Substring(0, 10) & Chr(34) 'id

           End If

           ExcelSheet.Columns.AutoFit()
           ExcelBook.Save()

推荐答案

没有SQL和Excel的比较.您在这里所做的就是定义excel字段并将值放入其中.因此,对于SQL,定义表,然后在上述事件中,使用ADO.NET连接到数据库,然后保存数据.试用.如果遇到任何问题,请发布特定的问题.
There is no comparison of SQL & Excel. All you are doing here is defining fields of excel and putting values in it. So, for SQL, have tables defined and then in the event above, connect to database using ADO.NET and then save your data. Try out. Post specific issue if you face any.


最有可能的是,这根本不是VB,因为这是VB.NET.如果这是.NET,则简短的答案是:忘记您的Excel代码并使用ADO.NET.这是一个很好的CodeProject入门文章:
为初学者使用ADO.NET [ http://en.wikipedia.org/wiki/ADO.NET [ http://msdn.microsoft.com/en-us/library/aa286484.aspx [ ^ ].

—SA
Most likely, this is not VB at all, because this is VB.NET. If this is .NET, the short answer is: forget your Excel code and use ADO.NET. This is a good introductory CodeProject article:
Using ADO.NET for beginners[^].

See also:
http://en.wikipedia.org/wiki/ADO.NET[^],
http://msdn.microsoft.com/en-us/library/aa286484.aspx[^].

—SA


这篇关于此代码从串行端口发送数据并保存在Excel中.如何更改此代码以保存在sql表中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆