Yazılım Eğitim

Yeni Haberler

Database operations using C # Class | Ebubekir Bastama


Good day, friend.

In this article, I will show you the automatic sql connection with class with C #.
At first we add a class to our project and we create a method that returns "SQLConnection".

        public SqlConnection connection()
        {
            
            if (con.State == ConnectionState.Closed)
            {
                con.Open();
            }
            return con;

        }

If the state of the connection is closed, we open the state of the connection. Now let's show how we use it.


  public void kolonverigetirme(string sqlcumle)
        {
            SqlCommand kmt = new SqlCommand(sqlcumle,connection());
            var s= kmt.ExecuteScalar();
            rowcount = int.Parse(s.ToString());           

        }
In this example, we wrote a method to use the class again, and we got the number of columns and a global variable. But as you can see, we did not say "con.open".
The only thing we do is "connection ()". Our connection was automatically turned on, as shown.

Hiç yorum yok