[英]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.