好的人我用DB写了我的第一个C#程序,我有问题。 我从ACCESS数据库中获取方向,我需要复制文件(“String my_querry”)。在Access中查询正在运行,但我的程序无法从中获取此字符串并从中复制文件...我做错了什么?附:对不起我的英文
这是代码:
static void Main(string[] args)
{
Microsoft.Office.Interop.Excel.Application excel = new Microsoft.Office.Interop.Excel.Application();
Microsoft.Office.Interop.Excel.Worksheet mysheet;
Microsoft.Office.Interop.Excel.Range myrange;
excel.Workbooks.Open("E:\\access\\Book1.xlsm");
//excel.Visible = true;
mysheet = excel.Worksheets[1];
myrange = mysheet.Cells;
int lastrow = mysheet.UsedRange.Count;
for (int i = 1; i <= lastrow; i++)
{
Console.WriteLine(myrange.Item[i, 1].Value);
string fileName = myrange.Item[i, 1].Value + ".pdf";
System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection();
conn.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;" +
@"Data source= E:\ACCESS\test.accdb";
conn.Open();
String my_querry = "SELECT adres FROM zrodla WHERE RIGHT(adres,21) = '" + fileName + "'";
OleDbCommand cmd = new OleDbCommand(my_querry, conn);
cmd.ExecuteNonQuery();
//DateTime ddd = DateTime.Now;
//string ddd2 = ddd.ToString("yyyy-MM-dd");
//string miesiac = ddd.ToString("MM");
//string dzien = ddd.ToString("dd");
string sourcePath = cmd.ToString();
string targetPath = @"E:\ACCESS\ho\";
if (System.IO.File.Exists(sourcePath+fileName))
{
string sourceFile = System.IO.Path.Combine(sourcePath, fileName);
string destFile = System.IO.Path.Combine(targetPath, fileName);
System.IO.File.Copy(sourceFile, destFile, true);
}
else
{
Console.Write("Plik nie istnieje!: " + fileName+"\n");
System.Data.OleDb.OleDbConnection conn2 = new System.Data.OleDb.OleDbConnection();
conn2.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;" +
@"Data source= E:\ACCESS\test.accdb";
conn2.Open();
String my_querry2 = "INSERT INTO dane(appid) VALUES('"+fileName+"')" ;
OleDbCommand cmd2 = new OleDbCommand(my_querry, conn2);
cmd2.ExecuteNonQuery();
}
}
}
答案 0 :(得分:1)
ExecuteNonQuery不会返回任何结果,而我怀疑你想获得查询的结果,而你只能通过使用cmd.ToString来完成。
正确的方法是使用ExecuteReader,它返回一个可用于检查结果的数据读取器。读者一次查看一行,您可以检查各列的内容,然后可以移动到下一行。我确信有足够的资源来帮助您与读者合作,例如https://msdn.microsoft.com/en-us/library/haa3afyz.aspx