Hi guys, i want to call a data from a database.. here is my code

string selectuser = "Dior";           

string dbconnection = Application.StartupPath + "\\Database1.mdf";
            SqlConnection conn = new SqlConnection(dbconnection);

            conn.Open();

            SqlCommand command = new SqlCommand("select username, nick from [user] where username=@user", conn);
            command.Parameters.AddWithValue("username", selectuser);
            SqlDataReader reader = command.ExecuteReader();

the thing is, i want to call a nick where the username is = select user.. Can you help me?

Recommended Answers

All 4 Replies

Change the parameters line to:

command.Parameters.AddWithValue("@user", selectuser);

@user is the parameter, username is the column.

Sorry maybe i misuderstood about the part.. could you tell me what is @ supposed to be and what is = supposed to be? And please there is something wrong with my dbconnection path.. using

string dbconnection = Application.StartupPath + "/Database1.mdf";

the dbconnection becomes

"C:\\Users\\Granodiorit\\Documents\\Visual Studio 2008\\Projects\\Apps\\Apps\\bin\\Debug/Database1.mdf"

and makes this code

SqlConnection conn = new SqlConnection(dbconnection);

got an ArgumentException was unhandled error.. Any idea?

If you have a dataBase in your project, you sure have a file called App.Config. You will find it in Solution explorer. There you have a connection string written, and also the name of the connection string. This is en example:

<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
  </configSections>
  <connectionStrings>
    <add name="myConnectionString"
        connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=C:\AppsTest\MyProject\mydataBaseName.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True"
        providerName="System.Data.SqlClient" />
  </connectionStrings>
</configuration>

so, when you have set the path to the dataBase and the it`s name(C# can do it by him self as well, while creating dataBase), you can easily use SqlConnection method.
I will show you an example how you have to do it, regarding your example.
But before going into the code, you need to add a new namespace to the class where you will use sql connection:
- in solution explorer right click on "References", and choose "Add reference"
- look for System.Configuration (and select it)
- on the top of the class there are namespaces which you use them - you have to add: using System.Configuration;

Now we can go to the code:

class YourClass
    {
        private static string sqlConn = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;
        //myConnectionString must be the same name as it is in the app.Config file!

           public static string[] SelectUser(string userName)
           {
                string value1 = null;  //username
                string value2 = null;  //nickname
                using (SqlConnection conn = new SqlConnection())
                {
                   string query = "select username, nick from [user] where username = @user";
                    using (SqlCommand cmd = new SqlCommand(query, conn))
                    {                       
                        cmd.Parameters.Add("@user", SqlDbType.VarChar, 50).Value = userName;
                        using(SqlDataReader reader = cmd.ExecuteReader())
                        {
                             conn.Open();
                             if(reader.Read())
                             {
                                  value1 = (string)reader[0]; 
                                  value2 = (string)reader[1];
                             }
                        }
                    }
                }
                return new string[]{ value1, value2 };
           }

IMPORTANT: try to use "using" statements, which automatically dispose (close) what has been opened!

So I hope this exmple will help you, its done very clearly so its easy to see what you need to get some data from database.

best regards,

Mitja

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.