我在课堂上有这段代码,它应该从文本框中读取 4 行,但是程序停止了,所以我不知道错误在哪里。我认为我的路还不错,但我几乎不需要改进它。
using System.Data.SqlClient;
using System.IO;
namespace tours
{
class myConnection
{
public static SqlConnection GetConnection()
{
string path = "C:\\Users\\marek\\Documents\\Visual Studio 2012\\Projects\\tours\\tours\\sql_string.txt";
StreamReader sr = new StreamReader(File.Open(path, FileMode.Open));
while (sr.Peek() >= 0)
{
}
string str = "Data Source='" + sr.ReadLine() + "';Initial Catalog ='" + sr.ReadLine() + "' ;user='" + sr.ReadLine() + "';password= '" + sr.ReadLine() + "'";
SqlConnection con = new SqlConnection(str);
con.Open();
return con;
}
}
}
这就是我如何称呼它的形式
private void nastaveni_Load(object sender, EventArgs e)
{
try
{
nacti_spojeni();
myConnection.GetConnection();
}
catch (Exception ex)
{
MessageBox.Show("" + ex);
}
}