這是我的代碼代碼不會沒有相對文件路徑工作
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.OleDb;
namespace SDD_Single_Project___Michael_Merjane
{
public partial class NewUser : Form
{
private OleDbConnection connection = new OleDbConnection(); //setting up a private connection
public NewUser()
{
InitializeComponent();
connection.ConnectionString = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=E:\schoolwork\Year 11\SDD\3 SINGLE TASK\SDD Single Project - Michael Merjane\SDD Single Project - Michael Merjane\bin\Persondata.accdb; //PROBLEM IS HERE
Persist Security Info=False;"; // there is no security for finding the location, this is not very safe but for the circumstances it works. In the line above, it is finding the location of the database. This could change due to computer and cause the whole program to not run
}
private void btnBack_Click(object sender, EventArgs e) //all of these mean when button is clicked
{
this.Hide(); //hides this page
MainScreen frm = new MainScreen(); //finds the next screen (the main screen)
frm.Show(); //shows it
}
private void btnSubmit_Click(object sender, EventArgs e)
{
try {
connection.Open(); // opens the connection
OleDbCommand command = new OleDbCommand(); //names command as a new oledbcommand for further use
command.Connection = connection;
command.CommandText = "insert into Persondata (FirstName,LastName,Address,Suburb,Email,Mobile,Gender,Age) values ('" + txtFirst.Text + "' , '" + txtLast.Text + "' , '" + txtAddress.Text + "' , '" + txtSuburb.Text + "' , '" + txtEmail.Text + "' , '" + txtMobile.Text + "' , '" + dropGender.Text + "' , '" + dropAge.Text + "') ";
// finds where its going to, finds the columns it is going to fill, finds the text boxes that is going to fill them
command.ExecuteNonQuery(); //execute the save
MessageBox.Show("Data Saved"); //pretty much shows a box saying everything worked
connection.Close(); // closes the connection
}
catch (Exception ex) //if something has gone wrong a catch will occur
{
MessageBox.Show("Error " + ex); //show the error
} //if there is a error message box will appear informing it
}
}
}
這是一個任務,我必須給代碼,問題是我不能夠把它因爲那樣的話絕對路徑不會找到該文件。我需要一種方法來使用由於位置更改而可能更改的相對文件路徑。目前,路徑(只要是這樣)進入程序文件中的bin文件夾。所以如果有一種方法來改變它,所以它會自動查找它自己的程序文件到bin或者它自己的程序文件中的任何其他地方,這很好。