c# 与 SQL 连接的相对路径

如: SqlConnection sql = new SqlConnection();
sql.ConnectionString = "Data Source=localhost;Initial Catalog=学生信息成绩;Integrated Security=True";
sql.Open();
因为要拿到其他电脑上用所以想写成相对路径,请各位大虾告知

第1个回答  2011-07-06
在右边的解决方案项目中添加新项,添加=>SQL 数据库,对这个数据库操作就OK了啊、追问

怎么对添加的数据源操作啊

追答

和你平时操作的数据库一样啊,不过这个数据库是独立出来放在你的解决方案项目里了罢了。
如代码:
using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
try
{
int i = 0;
string sqlStr = string.Format("select * from MyName");
string conStr = @"Data Source=.\SQLEXPRESS;AttachDbFilename=F:\exC#\ConsoleApplication1\ConsoleApplication1\HPJ.mdf;Integrated Security=True;User Instance=True";
SqlConnection conn = new SqlConnection(conStr);
conn.Open();
SqlCommand command = new SqlCommand(sqlStr, conn);
SqlDataReader reader = command.ExecuteReader();

while (reader.Read())
{

i++;
Console.WriteLine();
Console.WriteLine("---------------学生" + i + "---------------------");
Console.WriteLine("学号: " + reader["No"].ToString());
Console.WriteLine("姓名: " + reader["Name"].ToString());
Console.WriteLine("专业: " + reader["Specia"].ToString());
Console.WriteLine("爱好: " + reader["Hoppy"].ToString());
Console.WriteLine("----------------------------------------");
}
conn.Close();
}
catch (Exception ex)
{
Console.WriteLine("The error is that: " + ex.Message);
}
finally
{

}
Console.ReadLine();
}
}
}

本回答被提问者采纳
第2个回答  2011-07-06
拿到别人的电脑上直接改localhost就行了啊,哪来的相对不相对的问题