พอดีหัดเขียนให้มันเรียก connect จาก class อืน ครับ ผมผิดตรงไหนบ้างครับ
Connect_class.cs (C#)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;
namespace test_class
{
class Connect_class
{
public static SqlConnection conn = null;
public void Connect_class()
{
String SqlConn;
SqlConn = "SERVER=localhost;UID=inventory_db;PASSWORD=P@ssw0rd;DATABASE";
conn = new SqlConnection(SqlConn);
conn.Open();
}
}
}
Form1.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace test_class
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
Connect_class NewConnect = new Connect_class();
NewConnect.Connect_class();
SqlCommand com = new SqlCommand();
com.Connection = Connect_class.conn;
com.CommandText = "select * from dbo.Employee Where Employee_Username like '"+this.TextUsername.Text+"'and Employee_Password'"+this.textPassword.Text+"' ";
com.ExecuteNonQuery();
}
}
}