我已经从互联网上复制了一些代码并对其进行了修改我理解它正在做什么,但我无法将其记录到数据库中。如果我输入"变量的名称"我似乎无法得到除了我在值字段中键入的内容之外的任何内容。那就是我从另一端得到的东西。一旦我开始工作,它就完成了。
我得到的错误是数据库是打开的/或/插入执行命令行号上的变量有问题。
private void button9_Click(object sender, EventArgs e)
{
try
{
string ConnString = (@"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=C:\\temp\\names.accdb;Persist Security Info=False");
using (OleDbConnection Conn = new OleDbConnection(ConnString))
{
Conn.Close();//severl times connection has been open
Conn.Open();
DataSet ds = new DataSet();
ds.ReadXml(@"c:\\temp\\my123.xml");
OleDbCommand cmd = new OleDbCommand();
OleDbCommand cmd1 = new OleDbCommand();
DataTable dtCSV = new DataTable();
dtCSV = ds.Tables[0];
cmd.Connection = Conn;
cmd.CommandType = CommandType.Text;
cmd1.Connection = Conn;
cmd1.CommandType = CommandType.Text;
Conn.Open();
for (int row = 0; row <= dtCSV.Rows.Count - 1; row++)
{
for (int col = 1; col <= dtCSV.Columns.Count - 1; col++)
{
cmd.CommandText= ("INSERT INTO tab1 ( field1, field2) VALUES (dtCSV.Rows ,dtCSV.Columns)");
cmd.ExecuteNonQuery();
}
}
Conn.Close();
}
}
catch (Exception ex)
{
richTextBox1.Text = richTextBox1.Text + "\n Error " + ex + "\n"; ;
}
}
错误System.InvalidOperationException:连接不是 关闭。连接的当前状态是开放的。在 System.Data.ProviderBase.DbConnectionInternal.TryOpenConnection(的DbConnection outerConnection,DbConnectionFactory connectionFactory, TaskCompletionSource`1重试,DbConnectionOptions userOptions)at System.Data.ProviderBase.DbConnectionInternal.OpenConnection(的DbConnection outerConnection,DbConnectionFactory connectionFactory)at System.Data.OleDb.OleDbConnection.Open()at WindowsFormsApp5.Form1.button9_Click(Object sender,EventArgs e)
答案 0 :(得分:1)
您在连接对象上调用Open
函数两次,如下面的代码块所示。基本上,当连接状态仍为 Conn.Open(); <--- Here
DataSet ds = new DataSet();
ds.ReadXml(@"c:\\temp\\my123.xml");
OleDbCommand cmd = new OleDbCommand();
OleDbCommand cmd1 = new OleDbCommand();
DataTable dtCSV = new DataTable();
dtCSV = ds.Tables[0];
cmd.Connection = Conn;
cmd.CommandType = CommandType.Text;
cmd1.Connection = Conn;
cmd1.CommandType = CommandType.Text;
Conn.Open(); <--- Here
时,您正试图打开连接,并且错误地说的是
cmd1
正如@Steve所说,INSERT
从未使用过,看起来像是附加在同一命令文本中。
此外,您的"INSERT INTO tab1 ( field1, field2) VALUES (dtCSV.Rows ,dtCSV.Columns)"
查询永远不会按预期方式运行,因为您实际上并未取代值
string.Format("INSERT INTO tab1 ( field1, field2) VALUES ({0} ,{1})", dtCSV.Rows[row] ,dtCSV.Columns[col])
你应该认为它是
$"INSERT INTO tab1 ( field1, field2) VALUES ({dtCSV.Rows[row]} ,{dtCSV.Columns[col]})"
(或)如果您使用的是C#6,那么您可以说
Sub GetFolder_Data_Collection()
Range("A:L").ClearContents
Range("A1").Value = "Name"
Range("B1").Value = "Path"
Range("C1").Value = "Size (KB)"
Range("D1").Value = "DateLastModified"
Range("E1").Value = "Attributes"
Range("F1").Value = "DateCreated"
Range("G1").Value = "DateLastAccessed"
Range("H1").Value = "Drive"
Range("I1").Value = "ParentFolder"
Range("J1").Value = "ShortName"
Range("K1").Value = "ShortPath"
Range("L1").Value = "Type"
Range("A1").Select
Dim strPath As String
'strPath = "I:\Information Security\KRI Monthly Data Collection\"
strPath = GetFolder
Dim OBJ As Object, Folder As Object, File As Object
Set OBJ = CreateObject("Scripting.FileSystemObject")
Set Folder = OBJ.GetFolder(strPath)
Call ListFiles(Folder)
Dim SubFolder As Object
For Each SubFolder In Folder.SubFolders
Call ListFiles(SubFolder)
Call GetSubFolders(SubFolder)
Next SubFolder
End Sub
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub ListFiles(ByRef Folder As Object)
On Error Resume Next
For Each File In Folder.Files
ActiveCell.Offset(1, 0).Select
ActiveCell = File.Name
ActiveCell.Offset(0, 1).Select
ActiveCell.Offset(0, 1) = File.Path
ActiveCell.Offset(0, 0).Hyperlinks.Add Anchor:=ActiveCell.Offset(0, 0), Address:=File.Path, TextToDisplay:=File.Path
ActiveCell.Offset(0, -1).Select
ActiveCell.Offset(0, 2) = (File.Size / 1024) 'IN KB
ActiveCell.Offset(0, 3) = File.DateLastModified
ActiveCell.Offset(0, 4) = File.Attributes
ActiveCell.Offset(0, 5) = File.DateCreated
ActiveCell.Offset(0, 6) = File.DateLastAccessed
ActiveCell.Offset(0, 7) = File.Drive
ActiveCell.Offset(0, 8) = File.ParentFolder
ActiveCell.Offset(0, 9) = File.ShortName
ActiveCell.Offset(0, 10) = File.ShortPath
ActiveCell.Offset(0, 11) = File.Type
Next File
End Sub
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub GetSubFolders(ByRef SubFolder As Object)
Dim FolderItem As Object
On Error Resume Next
For Each FolderItem In SubFolder.SubFolders
Call ListFiles(FolderItem)
Call GetSubFolders(FolderItem)
Next FolderItem
End Sub
Function GetFolder() As String
Dim fldr As FileDialog
Dim sItem As String
Set fldr = Application.FileDialog(msoFileDialogFolderPicker)
With fldr
.Title = "Select a Folder"
.AllowMultiSelect = False
.InitialFileName = Application.DefaultFilePath
If .Show <> -1 Then GoTo NextCode
sItem = .SelectedItems(1)
End With
NextCode:
GetFolder = sItem
Set fldr = Nothing
End Function
答案 1 :(得分:0)
class MyClass : DependencyObject
{
public static readonly DependencyProperty MyDp = /* ... */
public MyClass()
{
this.ResetValue(MyDp);
/* ... */
}
};
你可以用它。我希望它可以帮到你。