我有一个保存在Web服务器文件夹中的Excel模板文件。 在运行期间,我使用变量(dunsno)和日期给出数据源 但它会引发错误:
初始化字符串的格式不符合从索引3开始的规范。
我只想知道如何在服务器端编写连接字符串
我做到了:
protected void Button1_Click(object sender, EventArgs e)
{
string connStr = "provider=Microsoft.Jet.OLEDB.4.0;Data Source='Server.MapPath(~/Profile/ + "Tempfile" + dunsno + DateTime.Today.ToString("dd.MM.yyyy") + ".xls");Extended Properties=Excel 8.0;";
DirectoryInfo directoryInfo = new DirectoryInfo(Server.MapPath("~/temp/"));
var fileList = directoryInfo.GetFiles();
string newFileName = Server.MapPath("~/Profile/" + "Tempfile" + dunsno + DateTime.Today.ToString("dd.MM.yyyy") + ".xls");
foreach (FileInfo fleInfo in fileList)
{
fleInfo.CopyTo(newFileName, true);
}
OleDbConnection MyConnection;
OleDbCommand MyCommand = new OleDbCommand();
MyConnection = new OleDbConnection(connStr);
MyConnection.Open();
MyCommand.Connection = MyConnection;
string sql = "Insert into [Sheet1$] (id,name) values('3','c')";
MyCommand.CommandText = sql;
MyCommand.ExecuteNonQuery();
MyConnection.Close();
}
更多的VBA指出要明确这一点:
Sub GrabLastNames()
'dimension (set aside memory for) our variables
Dim objIE As InternetExplorer
Dim ele As HTMLLinkElement
Dim e As HTMLLinkElement
Dim h As HTMLLinkElement
Dim t As HTMLTable
Dim s As String
Dim x, y, z
Dim c As Integer
Dim post As Object, Elem As Object
Set objIE = New InternetExplorer
objIE.Visible = True
i = 0
objIE.Navigate "http://apps.who.int/immunization_monitoring/globalsummary/countries?countrycriteria%5Bcountry%5D%5B%5D=ALB&commit=OK"
Do While objIE.Busy = True Or objIE.ReadyState <> 4: DoEvents: Loop
Set post = objIE.Document.getElementById("countrycriteria_country")
For Each Elem In post.getElementsByTagName("option")
c = c + 1
If c = 6 Then Exit For
Elem.Selected = True
objIE.Document.getElementById("countrycriteria_country").Focus
objIE.Document.getElementById("countrycriteria_country").FireEvent ("onchange")
Do While objIE.Busy = True Or objIE.ReadyState <> 4: DoEvents: Loop
For Each ele In objIE.Document.getElementsByTagName("table") '.getElementById("myTable").getElementsByTagName("tr")
s = LCase(Trim(ele.innerText))
If InStr(s, "immunization schedule") Or InStr(s, "percentage target population vaccinated by antigen") Then
For Each e In ele.Rows
i = i + 1
j = 3
' Sheet1.Cells(i, 1).Value = Elem.Value
For Each h In e.Cells
j = j + 1
Sheet1.Cells(i, j).Value = h.innerText
Next h
Next e
End If
Next ele
Next Elem
ActiveWorkbook.Save
End Sub
Sub Select_Item()
Dim post As Object, Elem As Object
With CreateObject("InternetExplorer.Application")
.Visible = True
.Navigate "http://apps.who.int/immunization_monitoring/globalsummary/countries?countrycriteria%5Bcountry%5D%5B%5D=ALB&commit=OK" ''stored the html elements within "element.html" file to test it locally
While .Busy = True Or .ReadyState < 4: DoEvents: Wend
Set post = .Document.getElementById("countrycriteria_country")
For Each Elem In post.getElementsByTagName("option")
If Elem.Value = "BHS" Then Elem.Selected = True: Exit For
' i = i + 1
' If i = 4 Then Exit For
Next Elem
Application.Wait (Now + TimeValue("0:00:02"))
.Document.getElementById("countrycriteria_country").Focus
Application.Wait (Now + TimeValue("0:00:02"))
'.Document.getElementById("countrycriteria_country").Change
.Document.getElementById("countrycriteria_country").FireEvent ("onchange")
End With
End Sub
' Loop vba========
' Elem.Selected = True
' objIE.Document.getElementById("countrycriteria_country").Focus
' objIE.Document.getElementById("countrycriteria_country").FireEvent ("onchange")
' Do While objIE.Busy = True Or objIE.ReadyState <> 4: DoEvents: Loop
Sub getCoverage()
Application.ScreenUpdating = False
Sheet1.Cells.Clear
'dimension (set aside memory for) our variables
Dim objIE As InternetExplorer
Dim ele As HTMLLinkElement
Dim e As HTMLLinkElement
Dim h As HTMLLinkElement
Dim t As HTMLTable
Dim s As String
Dim x, y, z
Dim con(1000)
Dim c As Integer
Dim post As Object, Elem As Object
Dim s1 As String, s2 As String, s3 As String, s4 As String
Set objIE = New InternetExplorer
objIE.Visible = True
' Do While objIE.Busy = True Or objIE.ReadyState <> 4: DoEvents: Loop
s1 = LCase(shtweb.Range("d5").Value)
s2 = LCase(shtweb.Range("e5").Value)
s3 = LCase(shtweb.Range("f5").Value)
s4 = LCase(shtweb.Range("g5").Value)
s2 = IIf(s2 = "", "fsdfswew", s2)
s3 = IIf(s3 = "", "fsdfswew", s3)
s4 = IIf(s4 = "", "fsdfswew", s4)
i = 0
objIE.Navigate "http://apps.who.int/immunization_monitoring/globalsummary/countries?countrycriteria%5Bcountry%5D%5B%5D=ALB&commit=OK"
Do While objIE.Busy = True Or objIE.ReadyState <> 4: DoEvents: Loop
Set post = objIE.Document.getElementById("countrycriteria_country")
For Each Elem In post.getElementsByTagName("option")
con(c) = Elem.Value
c = c + 1
Next Elem
'objIE.
For k = 0 To c
If con(k) = "" Then Exit For
objIE.Navigate "http://apps.who.int/immunization_monitoring/globalsummary/countries?countrycriteria%5Bcountry%5D%5B%5D=" & con(k) & "&commit=OK"
Do While objIE.Busy = True Or objIE.ReadyState <> 4: DoEvents: Loop
For Each ele In objIE.Document.getElementsByTagName("table") '.getElementById("myTable").getElementsByTagName("tr")
s = LCase(Trim(ele.innerText))
If InStr(s, s1) Or InStr(s, s2) Or InStr(s, s3) Or InStr(s, s4) Then
For Each e In ele.Rows
i = i + 1
j = 3
Sheet1.Cells(i, 1).Value = con(k)
For Each h In e.Cells
j = j + 1
If Left(h.innerText, 1) = "=" Then
Sheet1.Cells(i, j).Value = "'" & h.innerText
Else
Sheet1.Cells(i, j).Value = h.innerText
End If
Next h
Next e
End If
Next ele
Next k
ActiveWorkbook.Save
Application.ScreenUpdating = True
End Sub
答案 0 :(得分:0)
使用Path.Combine
和OleDbConnectionStringBuilder
创建有效的连接字符串:
var conString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0; Data Source={0}"
, Path.Combine(dir, "nik.mdb"));
var conBuilder = new OleDbConnectionStringBuilder(conString);
using (var con = new OleDbConnection(conBuilder.ConnectionString))
{
// ...
}
也许连接字符串中有空格。