c# - How do I create a class with SQL connection and query functions and calling it to my windows form buttons? -
i want create class has sql connection , functions (like insert, select, delete queries) , want call forms (buttons , etc.)
i don't know if it's possible or not or maybe there ways on doing so...
i've done research , come code on class sql connection , i'm not sure if it's correct.
thank in advance. i'm beginner , want learn more on c#.
any type of response appreciated. thank you
sorry bad english
using system.data.sqlclient; class sqlconnclass { public static sqlconnection getconnection() { string str = "data source=localhost;initial catalog=kwem;integrated security=true;"; sqlconnection conn = new sqlconnection(str); conn.open(); return conn; }
you close! may want take `conn.open()' out of method can open query. (remember close or put in using statement!)
public static void updatedb(string valtoupdate) { sqlconnection conn = getconnection(); using (conn) { sqlcommand updatecommand = new sqlcommand(getconnection(), "update table set val = @newvalue"); updatecommand.parameters.addwithvalue("@newvalue", valtoupdate); conn.open(); updatecommand.executenonquery(); } }
you same other kind of db functions.
Comments
Post a Comment