テキストボックスから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);
}
}