I wrote a C# program to output the contents what is contained in database tables, but when I append conn.open()
, dialog box doesn't be created although there isn't any compile error. I already checked whether or not the setting of database information such as ip address, user name, pass word, and database name, but there isn't any misses. I wonder why it isn't executed as soon as appending conn.open()
:
public static string constring = "Data Source= 192.168.0.21; User=root; Password=admin;database=hwg;";
SqlConnection conn = new SqlConnection();
private string strConnString = "";
[public void ConnectDB]
strConnString = constring;
if (conn.State.ToString().Equals("Closed"))
{
conn.ConnectionString = strConnString;
conn.Open(); //problem on this line
if (conn.State == ConnectionState.Open)
{
}
else
{
conn.Close();
}
}
[public DataTable GetDBTable(string sql)]
SqlDataAdapter adapter = new SqlDataAdapter(sql, conn);
SqlCommandBuilder builder = new SqlCommandBuilder(adapter);
DataTable dt = new DataTable();
adapter.Fill(dt);
return dt;
[public Form1()]
InitializeComponent();
db = new Con_database();
db.ConnectDB();
[private void button1_Click(object sender, EventArgs e)]/*If we click button1, the overall contents of applied table in database hwg is listed*/
string sql = "SELECT * FROM id_repository";
DataTable dt = db.GetDBTable(sql);
DatabaseInquiry.DataSource = dt;
db.ClosedDB();