这是我的代码
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或其自身程序文件中的任何其他地方,这将是非常好的。
答案 0 :(得分:1)
尝试:
var currDir = System.Environment.CurrentDirectory;
然后从那里连接路径......
答案 1 :(得分:1)
将您想要的任何文件放入当前页面所在的文件夹中。然后
Directory.GetCurrentDirectory()
将提供您正在处理的当前文件夹。它将为您提供项目的release文件夹。将其存储为字符串并在需要的地方使用它。
答案 2 :(得分:0)
不确定。 这是非常基本的事情 - 我建议将数据库文件放在bin中的DB文件夹中 - 或者保留在bin内部。
然后你需要确定你的二进制文件夹的位置 - 有几种方法,而下面两种最常见:
Environment.CurrentDirectory
- 直到您在运行期间(其他地方)不更改它Assembly.GetEntryAssembly().Location
- 这是启动当前进程的可执行文件的完整路径我建议然后查看System.IO.Path
类 - 首先从Location
中删除路径,然后将其组合回来,但这次是数据库文件名string
。
虽然这是你的任务,但我会让你自己去学习这门课程 - 这很有意思:P
http://msdn.microsoft.com/en-us/library/system.io.path(v=vs.110).aspx