我正在尝试使用VB.net使用变量创建一个SQL表。
唯一的问题是表名是文件名的派生,有时可能包含空格。
以下是我的代码示例:
Dim openFileDialog1 As New OpenFileDialog
Dim strFileName As String
Dim FileNameOnly As String
openFileDialog1.InitialDirectory = "C:\***\***\***"
openFileDialog1.Filter = "CSV files (*.csv)|*.csv"
openFileDialog1.FilterIndex = 2
openFileDialog1.RestoreDirectory = True
If openFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
End If
strFileName = openFileDialog1.SafeFileName
If strFileName <> "" Then
FileNameOnly = System.IO.Path.GetFileNameWithoutExtension(openFileDialog1.FileName)
TextBox1.Text = FileNameOnly
Using con = New SqlConnection("server=***\SQLEXPRESS; database=***Billing; integrated security=yes")
Using cmda = New SqlCommand("CREATE TABLE '" + FileNameOnly + "' (CallType VarChar(30),ChargeCode VarChar(30),Destination VarChar(30),TariffUsed VarChar(30),Peak Float,OffPeak Float,Weekend Float,Setup Float,MinimumCharge Float,ChargeCap INT,InitialUnits INT,InitialCharge INT,InitialPeak INT,InitialOffPeak INT,InitialWeekend INT,BillingUnit INT,MinimumUnits INT,RateType VarChar(30));", con)
con.Open()
cmda.ExecuteNonQuery()
con.Close()
End Using
End Using
......这是我得到的错误
&#34;其他信息:FFA68878 CPS费率附近的语法错误&#39;。&#34;
安全文件名为&#34; FFA68878 CPS费率&#34;
任何人都可以帮助使用正确的语法吗?
任何帮助都非常感谢:)
答案 0 :(得分:2)
将表格和列名称(带空格)放在方括号内,如
Using cmda = New SqlCommand("CREATE TABLE [" + FileNameOnly + "] (CallType . . .RateType VarChar(30));", con)
注意。 。 [&#34; + FileNameOnly +&#34;]。 。 上面。