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

Popular posts from this blog

node.js - Node js - Trying to send POST request, but it is not loading javascript content -

javascript - Replicate keyboard event with html button -

javascript - Web audio api 5.1 surround example not working in firefox -