Click here to Skip to main content
15,893,588 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi
i have a class with this code


C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data.SqlClient;
using System.Data;

public class DataAccess
{
    SqlConnection _MyConnection = new SqlConnection();
    SqlCommand _MyCommand = new SqlCommand();
    SqlDataAdapter _MyAdapter = new SqlDataAdapter();

    private bool OpenConnection()
    {
        try
        {
            _MyConnection.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|ASPNETDB.MDF;Integrated Security=True;User Instance=True";
            if (_MyConnection.State == System.Data.ConnectionState.Closed)
            {
                _MyConnection.Open();
                return true;
            }
            return false;
        }
        catch 
        { 
            return false;
        }
   
    }
    private void CloseConnection()
    {
        _MyConnection.Close();
    }

    public int ExeIUDQuery(string Query)
    {
        OpenConnection();
        _MyCommand.Connection = _MyConnection;
        _MyCommand.CommandText = Query;
        int RowAffectedCount = _MyCommand.ExecuteNonQuery();
        CloseConnection();
        return RowAffectedCount;
    }

    public DataSet ExeSQuery(string Query)
    {
        OpenConnection();
        _MyCommand.Connection = _MyConnection;
        _MyCommand.CommandText = Query;
        _MyAdapter.SelectCommand = _MyCommand;
        DataSet __DS = new DataSet();
        _MyAdapter.Fill(__DS);
        CloseConnection();
        return __DS;
    }
    public DataAccess()
	{
		
	}
}

and for example a form with insert data by this code :

C#
protected void Button1_Click(object sender, EventArgs e)
    {

        string user = Txtuser.Text;
        string pas = Txtpass.Text;
        string query = "";
        query = "INSERT INTO a (name,family) VALUES ( + user + ,+ pas+ );
        DataAccess DA = new DataAccess();
        DA.ExeIUDQuery(query);
    }

how to use a storedprocedur for this program
i need help for create storedprocidured for program
pleas help me
tanks
Posted
Updated 21-Jul-12 11:59am
v2

1 solution

None of this makes any sense. This is horrible code.

1 - you never factor things out to the point that you can run ANY SQL, you write strongly typed code that knows what it's doing
2 - You NEVER pass raw text in to SQL, by doing this, you open yourself to SQL injection attacks. I can delete your whole database by using this form
3 - You make your data access class static, you don't want to have to create it every time you make a call

How to write stored procs is widely documented on the web. How to call them is also widely reported. It's clear from this that you don't have any 'need' in the sense that you're clearly just teaching yourself, no-one could be paying for this code. So, I suggest you buy some books and work through them and learn the right way to write a data layer, as well as learning how to execute stored procs etc, on the way. There's lots of online articles, too, and they will go in to more depth than a forum reply, and you clearly need all the help you can get.
 
Share this answer
 
Comments
Espen Harlinn 22-Jul-12 14:13pm    
Good points :-D

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900