简体   繁体   English

如何在Ole Db连接中的文本框中指定文件路径名

[英]How to specify file path name from text box in Ole Db connection

I am using the bellow code to import .csv file in to datagridview ..., i am getting an error ie g_03_04_2014.csv is not a valid path . 我正在使用下面的代码将.csv文件导入到datagridview ...,我收到一个错误,即g_03_04_2014.csv不是有效路径。 ... ...

   private void timer2_Tick(object sender, EventArgs e)
   {


       string dt = DateTime.Now.ToString("hh.mm.ss.ffffff");
       string var1 = (dt);
       string var2 = adsClient.ReadAny(hActVel, typeof(double)).ToString(); ;
       string var3 = adsClient.ReadAny(hSActVel, typeof(double)).ToString(); ;
       StreamWriter sw = File.AppendText(NameYourFile.Text + "_" + DateTime.Today.ToString("MM_dd_yyyy") + ".csv");


       {

           if (BigMotorActualVelocity.Checked && SmallMotorActualVelocity.Checked)
           {
               sw.WriteLine(string.Format("{0},{1},{2}",var1, var2, var3));

           }

           else if (BigMotorActualVelocity.Checked)
           {
               sw.WriteLine(string.Format("{0},{1}",var1 , var2));
           }

           else if (SmallMotorActualVelocity.Checked)
           {
               sw.WriteLine(string.Format("{0},{1},{2}",var1," ",var3));
           }
           else
           {
               sw.WriteLine(string.Format("{0}",var1));
           }

           sw.Dispose();

       }


       string filepathName = NameYourFile.Text + "_" + DateTime.Now.ToString("MM_dd_yyyy") + ".csv";
       string sconn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
           filepathName + ";Extended Properties='text;HDR=Yes;FMT=Delimited(,)';";

       OleDbConnection connection = new OleDbConnection(sconn);

       OleDbCommand command = new OleDbCommand("Select * From ["+Path.GetFileName(filepathName) +"]",connection);

       DataSet ds = new DataSet();
       OleDbDataAdapter adapter = new OleDbDataAdapter(command);

       adapter.Fill(ds);
       dataGridView1.DataSource =  ds.Tables[0];

   }                                                                                     Thank you...  

File.AppendText method takes full path as a parameter, not the file name. File.AppendText方法采用完整路径作为参数,而不是文件名。

public static StreamWriter AppendText(
    string path
)

Parameters
Type: System.String
The path to the file to append to. 

g_03_04_2014.csv is just a file name, not path. g_03_04_2014.csv只是一个文件名,而不是路径。

You will need to provide complete file path in the connection string for this. 为此,您需要在连接字符串中提供完整的文件路径。 If you only provide name, application will look for the file in the same directory as the executable. 如果仅提供名称,则应用程序将在与可执行文件相同的目录中查找文件。

You are not setting up your connection string correctly. 您没有正确设置连接字符串。
When reading from a CSV file, your datasource is the directory where your file resides and the table is the file name 从CSV文件读取时,数据源是文件所在的目录,表是文件名

string fileName = NameYourFile.Text + "_" + DateTime.Now.ToString("MM_dd_yyyy") + ".csv";
string folderName = Application.StartupPath;  
string sconn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
       folderName + ";Extended Properties='text;HDR=Yes;FMT=Delimited(,)';";

OleDbCommand command = new OleDbCommand("Select * From ["+fileName+"]",connection);

Lacking better context, I have used the Application.StartupPath as the folder where you have saved the CSV file, however, I really suggest to change this location to a place better suited to store user data files and free from permission problems. 缺少更好的上下文,我将Application.StartupPath用作保存CSV文件的文件夹,但是,我确实建议将该位置更改为更适合存储用户数据文件且没有权限问题的位置。

Use Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData) as a base path for save and read then create your dedicated folder there where you store your files 使用Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData)作为保存和读取的基本路径,然后在此处存储文件的专用文件夹中创建

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何获得提供者名称以打开Ole DB连接? - How do I get provider name to open Ole DB connection? 选择路径名称时,如何从文本框中的组合框中获取选定的数据库名称 - How do I get selected database name from Combo box in a text box when I choose the Path name 在文本框中显示文件名而不是完整路径 - show file name instead of full path in text box 如何使用 C# Web 应用程序将选定路径中的文件夹名称读取到文本框中 - How to read Folder name from a selected path into a text box using C# web application 如何使用文本框中的输入作为文件名来命名.csv文件? - How to Name a .csv file using the input from a text box as the file name? 永久更改OLE DB连接的数据库 - Permanently change database of an OLE DB Connection 如何用路径指定配置文件? - How to specify a config file with path? 我们可以使用FolderBrowser对话框选择特定文件,然后将该文件的路径及其名称复制到文本框中吗? - Can we choose a particular file using folderbrowser dialogue and then copy the path of that file with its name to a text box 如何从文本文件中选择某个文本并输入文本框 - How to select a certain text from text file and enter into text box 增加OLE DB连接字符串中的连接池大小 - Increase connection pool size in OLE DB connection string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM