1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

i use this code to connect to mysql database but how can i get the result in array

Discussion in 'C#' started by ramysarwat, Feb 16, 2010.

  1. #1
    i use this code to connect to mysql database but how can i get the result in array and not in datagrid

     
    ramysarwat, Feb 16, 2010 IP
  2. gangwisch

    gangwisch Peon

    Messages:
    66
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Here is some sample code to use:

     
    gangwisch, Feb 17, 2010 IP
  3. cyberpope

    cyberpope Greenhorn

    Messages:
    86
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    18
    #3
    I am better at c# than vb but I will break down your code so you see what it is doing
    I am better at c# than VB, I will walk through your code and tell you what it is doing. This may make it easier for you to change

    This is creating a string object
    "Dim strConn As String"
    This sets the value of the string to represent the parameters to connect to your db
    "strConn = "DRIVER={MySQL};SERVER=yourip;DATABASE=dbname;" & _
    "USER=username;PASSWORD=pass; OPTION=3;""

    Another string with your SQL query
    "Dim MySQL As String = "select * from Customers""
    Here you are creating a connection object that will be used to connect to your db
    "Dim MyConn As New OdbcConnection(strConn)"
    You are now creating a dataset object. I do not knwo why you want an array, you can parse or bind this object to get what you want. A dataset houses rows, tables. You can think of them as a collection, similiar to an array.
    Dim ds As DataSet = New DataSet()

    This is the command you will send to theconnection
    Dim Cmd As New OdbcDataAdapter(MySQL, MyConn)
    This is when you populate the dataset. Set a breakpoint before and after and you can explore and axctually look at the data
    Cmd.Fill(ds, "Customers")
    This will be the code you replace. This is when you bind your dataset. If you want to maybe just parse through the dataset one row at a time and then just populate your webpage instead of using a grid you can do that. With components like a grid it is just less work.
    MyDataGrid.Datasource = ds.Tables("Customers").DefaultView
    MyDataGrid.DataBind()


    What are you trying to achieve with the array and I may be able to hep you.

    "
     
    cyberpope, Feb 18, 2010 IP
  4. ramysarwat

    ramysarwat Peon

    Messages:
    164
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    thank you gangwisch and cyberpope for your help

    but i keep getting this error: ExecuteReader requires an open and available Connection. The connection's current state is closed.

    i get the connection string from godaddy so i dont know where is the problem
     
    ramysarwat, Feb 19, 2010 IP
  5. cyberpope

    cyberpope Greenhorn

    Messages:
    86
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    18
    #5
    Test the connection with a sql manager. If you have VS express it has one in it.

    What ide are\ you using? You probably have to execote teh connect method. I have not done vb since visual studio 6.0, I am all c#.

    Do you have any screen sharing programs? I coudl potentially help you debug it is we can set up screen sharing. I can send you code to connet to a godaddy MS SQL database, that works if you need it.
     
    cyberpope, Feb 19, 2010 IP
  6. cyberpope

    cyberpope Greenhorn

    Messages:
    86
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    18
    #6
    Here is some code I found online showing in vb how to do the pasrsing

    set variable to number of rows in dataset -1 since it starts at zero. Without the -1 you will get an error
    For X = 0 to DataSet.Tables(0).Rows.Count - 1

    This code seems to just compare a columnname to itself. So I am assumign it is just showing you how to access the check it for some value for iteration purposes.
    If DataSet.Tables(0).Rows(X)("ColumnName").ToString = DataSet.Tables(0).Rows(X)("ColumnName").ToString Then

    'Do stuff

    End If

    go to next row.

    Next X
     
    cyberpope, Feb 19, 2010 IP
  7. cyberpope

    cyberpope Greenhorn

    Messages:
    86
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    18
    #7
    Just making sure, she you set this code you place the actual database info srver, your uid and pwd in it correct? LOL

    strConn = "DRIVER={MySQL};SERVER=yourip;DATABASE=dbname;" & _
    "USER=username;PASSWORD=pass; OPTION=3;"
     
    cyberpope, Feb 19, 2010 IP
  8. cyberpope

    cyberpope Greenhorn

    Messages:
    86
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    18
    #8
    Here is code frm microsft
    Dim nwindConn As SqlConnection = New SqlConnection("Data Source=localhost;Integrated Security=SSPI;Initial Catalog=northwind")

    Dim selectCMD As SqlCommand = New SqlCommand("SELECT CustomerID, CompanyName FROM Customers", nwindConn)
    selectCMD.CommandTimeout = 30

    Dim custDA As SqlDataAdapter = New SqlDataAdapter
    custDA.SelectCommand = selectCMD

    nwindConn.Open()

    Dim custDS As DataSet = New DataSet
    custDA.Fill(custDS, "Customers")

    nwindConn.Close()
    [C#]
    SqlConnection nwindConn = new SqlConnection("Data Source=localhost;Integrated Security=SSPI;Initial Catalog=northwind");

    SqlCommand selectCMD = new SqlCommand("SELECT CustomerID, CompanyName FROM Customers", nwindConn);
    selectCMD.CommandTimeout = 30;

    SqlDataAdapter custDA = new SqlDataAdapter();
    custDA.SelectCommand = selectCMD;

    nwindConn.Open();

    DataSet custDS = new DataSet();
    custDA.Fill(custDS, "Customers");

    nwindConn.Close();

    You will change this to put in the godaddy soecifc connection info

    SqlConnection nwindConn = new SqlConnection("Data Source=localhost;Integrated Security=SSPI;Initial Catalog=northwind");

    It will be like this but with your uid and pwd I believe
    SqlConnection nwindConn = new SqlConnection("DRIVER={MySQL};SERVER=yourip;DATABASE=dbname;" & _
    "USER=username;PASSWORD=pass; OPTION=3;" );

    Change this section to have your query

    SqlCommand selectCMD = new SqlCommand("SELECT CustomerID, CompanyName FROM Customers", nwindConn);

    change this to your table name
    custDA.Fill(custDS, "Customers");
     
    cyberpope, Feb 19, 2010 IP
  9. chengary

    chengary Banned

    Messages:
    88
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #9
    Server=localhost;User ID=root;Password=sa;Database=_Database;CharSet=utf8;
     
    chengary, Feb 20, 2010 IP