根据你数据库放的位置,然后进行路径转换,关键在与你数据库放的地方
C# 获取路径
string str1 =Process.GetCurrentProcess().MainModule.FileName;//获得当前执行的exe的文件名。
string str2=Environment.CurrentDirectory;//获取和设置当前目录的完全限定路径。
string str3=Directory.GetCurrentDirectory();//获取应用程序的当前工作目录。
string str4=AppDomain.CurrentDomain.BaseDirectory;//获取基目录,它由程序集冲突解决程序用来探测程序集。
string str5=Application.StartupPath;//获取启动了应用程序的可执行文件的路径,不包括可执行文件的名称。
string str6=Application.ExecutablePath;//获取启动了应用程序的可执行文件的路径,包括可执行文件的名称。
string str7=AppDomain.CurrentDomain.SetupInformation.ApplicationBase;//获取或设置包含该应用程序的目录的名称。
“Application.StartupPath”:获取当前应用程序所在目录的路径,最后不包含“\”;
“Application.ExecutablePath ”:获取当前应用程序文件的路径,包含文件的名称;
“AppDomain.CurrentDomain.BaseDirectory”:获取当前应用程序所在目录的路径,最后包含“\”;
“System.Threading.Thread.GetDomain().BaseDirectory”:获取当前应用程序所在目录的路径,最后包含“\”;
“Environment.CurrentDirectory”:获取当前应用程序的路径,最后不包含“\”;
“System.IO.Directory.GetCurrentDirectory”:获取当前应用程序的路径,最后不包含“\”;
你的程序如果是winform的,把你的数据库放在bin\debug目录下
Application.StartupPath+\\11.mdb
如果是web的,用这种方法转换路径
HttpContext.Current.Server.MapPath();
实例运用
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace _5
{
public class connection
{
public static string connectingstring = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + System.AppDomain.CurrentDomain.BaseDirectory + @"database/student.mdb";
}
}
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Data.OleDb;
namespace _5
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
/// <summary>
/// 数据的导入
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
try
{
using (FileStream filestream = new FileStream(@"student.txt", FileMode.Open, FileAccess.ReadWrite))
{
//没有FileAccess.ReadWrite&&Encoding.Default 则汉字是乱码
using (StreamReader sr = new StreamReader(filestream, Encoding.Default))
{
using (OleDbConnection conn = new OleDbConnection(connection.connectingstring))
{
conn.Open();
using (OleDbCommand cmd = conn.CreateCommand())
{
string str = string.Empty;
while ((str = sr.ReadLine()) != null)
{
string[] data = str.Split('|');
cmd.CommandText = "INSERT INTO T_Students (name, age, score) VALUES (@name,@age,@score)";
cmd.Parameters.Clear();
cmd.Parameters.Add("@name", OleDbType.Char, 50).Value = data[0];
cmd.Parameters.Add("@age", OleDbType.BigInt, 50).Value = data[1];
cmd.Parameters.Add("@score", OleDbType.BigInt, 50).Value = data[2];
cmd.ExecuteNonQuery();
}
}
}
}
}
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
/// <summary>
/// 获得平均值
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button2_Click(object sender, EventArgs e)
{
using (OleDbConnection conn = new OleDbConnection(connection.connectingstring))
{
conn.Open();
try
{
using (OleDbCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "SELECT AVG(score) FROM T_Students";
if (string.IsNullOrEmpty(cmd.ExecuteScalar().ToString()))
{
this.textBox1.Text = "无数据:" + "0";
}
else
{
this.textBox1.Text = cmd.ExecuteScalar().ToString().Substring(0, 6);
}
}
}
catch (Exception)
{
conn.Close();
throw;
}
}
}
/// <summary>
/// 检索信息
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button3_Click(object sender, EventArgs e)
{
if (string.IsNullOrEmpty(this.txtScore.Text))
{
return;
}
using (OleDbConnection conn = new OleDbConnection(connection.connectingstring))
{
conn.Open();
try
{
using (OleDbCommand cmd = conn.CreateCommand())
{
cmd.CommandText = "SELECT name, score FROM T_Students WHERE score>=" + this.txtScore.Text + "";
OleDbDataReader odr = cmd.ExecuteReader();
while (odr.Read())
{
this.textBox3.Text += "姓名:" + odr.GetString(0) + " 成绩:" + odr.GetInt32(1) + "\r\n";
}
}
}
catch (Exception)
{
conn.Close();
throw;
}
}
}
}
}
|