Results 1 to 6 of 6

Thread: How can i connect ‘C’ language to a database

  1. #1
    Join Date
    Dec 2010
    Posts
    68

    How can i connect ‘C’ language to a database

    Is it possible to connect ‘c’ language with database, if yes than how?. I’m making a project in my last year of bsc-it, and I’m using ‘c’ language. I don’t know much about the connection. Please give me the step by step procedure of connection. , I’ve never done connection in ‘c’. please, I need your help. my project contains of 200 marks. Your small help will be very helpful for me.

  2. #2
    Join Date
    Nov 2009
    Posts
    792

    Re: How can i connect ‘C’ language to a database

    yes, you can connect ‘c’ language to a database, but You have not mention the name of a database which you want to connect from ‘c’ . you have also not mention the operating system which you want to use, so I can’t give you the proper information. The C languages do not have standard libraries to manage databases, you need to use the libraries provided by the compiler, or database vendors or any 3rd party libraries to connect and manage databases.You can find libraries and examples of connecting to MySQL, Oracle and Sybase databases from internet.

  3. #3
    Join Date
    Apr 2009
    Posts
    994

    Re: How can i connect ‘C’ language to a database

    If you are using linux or unix platform. Here is the small program that connects to mysql server and list tables from mysql database .
    /* Simple C program that connects to MySQL Database server*/
    #include <mysql.h>
    #include <stdio.h>

    main() {
    MYSQL *conn;
    MYSQL_RES *res;
    MYSQL_ROW row;

    char *server = "localhost";
    char *user = "root";
    char *password = "PASSWORD"; /* set me first */
    char *database = "mysql";

    conn = mysql_init(NULL);

    /* Connect to database */
    if (!mysql_real_connect(conn, server,
    user, password, database, 0, NULL, 0)) {
    fprintf(stderr, "%s\n", mysql_error(conn));
    exit(1);
    }

    /* send SQL query */
    if (mysql_query(conn, "show tables")) {
    fprintf(stderr, "%s\n", mysql_error(conn));
    exit(1);
    }

    res = mysql_use_result(conn);

    /* output table name */
    printf("MySQL Tables in mysql database:\n");
    while ((row = mysql_fetch_row(res)) != NULL)
    printf("%s \n", row[0]);

    /* close connection */
    mysql_free_result(res);
    mysql_close(conn);
    }

  4. #4
    Join Date
    Nov 2009
    Posts
    865

    Re: How can i connect ‘C’ language to a database

    To connect ‘c’ to a database write the following code in your program.
    <PRE lang=cs>string dbName = "testdb";

    string serverName = "(local)";

    string sapwd = "sapwd";</PRE><PRE lang=cs><PRE lang=cs>/// Using the standard constructor and inputing all information
    /// From the constructor. This will automatically instantiate the setConnection Member
    </PRE><PRE lang=cs>private dbx32sql.Connect conn = new dbx32sql.Connect(dbName, serverName, sapwd);
    </PRE></PRE><PRE lang=cs>/// Using the non standard constructor with no inputs. Must initiate setConnection;

    private dbx32sql.Connect conn = new dbx32sql.Connect();
    conn.Database = dbName;
    conn.Server = serverName;
    conn.Sapwd = sapwd;
    conn.setConnection;
    try
    {
    if (conn.Open)
    {
    string sqlGet = "SELECT COUNT (ID) FROM authors";
    SqlCommand cmdGet = new SqlCommand(sqlGet, conn.Connection);
    int Count = (int)cmdGet.ExecuteScalar();
    CmdGet.Dispose();
    }
    else
    {
    // Input your error handling here;
    }
    }
    catch(Exception ex)
    {
    //insert other error handling.
    }
    finally
    {
    conn.Close();
    }</PRE>

  5. #5
    Join Date
    Apr 2009
    Posts
    1,107

    Re: How can i connect ‘C’ language to a database

    The answer is yes, You can. But you have not mention the name of a database. Well, following are a sample program to connect ‘c’ with a oracle:
    using System;
    using System.Data;
    using System.Data.ADO;
    class OracleTest
    {
    static void Main()
    {
    //UID - User Id
    //PWD - Password
    //Server - Your Service name
    //Using Microsoft ODBC for Oracle DSN-Less
    //const string strConn = "Provider=MSDASQL;DRIVER={Microsoft ODBC for ORACLE};UID=scott;PWD=tiger;Server=fnqa";
    //Using Microsoft OLEDB Provider for Oracle
    const string strConn = "Provider=MSDAORA.1;
    Data Source=oracle_db;User ID=scott;Password=tiger";
    //Using DSN Connection
    //const string strConn ="DSN=oracle_dsn;UID=scott;PWD=tiger" ;
    const string strSQL = "Select * From EMP";
    try
    {
    DataSet ds = new DataSet("EmployeesSet");
    ADODataSetCommand myCommand = new ADODataSetCommand(strSQL,strConn);
    myCommand.FillDataSet(ds,"Buyer");
    Console.WriteLine(ds.Xml);
    }
    catch(ADOException e)
    {
    Console.WriteLine("Exception Occured :{0}",e.Errors[0].Message);
    }
    }
    }

  6. #6
    Join Date
    May 2008
    Posts
    1,020

    Re: How can i connect ‘C’ language to a database

    you can connect ‘c’ with a database using ODBC following are the sample program of that

    #include <Windows.h>
    #include <sql.h>
    #include <sqlext.h>
    #include <sqltypes.h>
    #include <stdio.h>

    //
    // function prototypes
    //
    int main();

    //
    // main
    //
    int main()
    {
    SQLHENV henv;
    SQLHDBC hdbc;
    SQLHSTMT hstmt;
    SQLRETURN sqlrc;
    char sql[1025];
    char svalue[2049];
    SQLINTEGER inum;
    SQLSMALLINT cols;
    int i;
    char uid[24];
    char pwd[24];
    char dsn[24];

    // connection strings
    strcpy(uid, "uid");
    strcpy(pwd, "pwd");
    strcpy(dsn, "dsn");

    // SQL strings
    strcpy(sql, "select * from kemp");

    // environment handle
    sqlrc = SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);
    if (sqlrc != SQL_SUCCESS && sqlrc != SQL_SUCCESS_WITH_INFO) {
    printf("SQLAllocHandle SQL_HANDLE_ENV Error\n");
    return 9;
    }

    // environment variables settings
    sqlrc = SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, (void *)SQL_OV_ODBC3, 0);
    if (sqlrc != SQL_SUCCESS && sqlrc != SQL_SUCCESS_WITH_INFO) {
    printf("SQLSetEnvAttr Error\n");
    return 9;
    }

    // connection handle
    sqlrc = SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc);
    if (sqlrc != SQL_SUCCESS && sqlrc != SQL_SUCCESS_WITH_INFO) {
    printf("SQLAllocHandle SQL_HANDLE_DBC Error\n");
    return 9;
    }

    // connect
    sqlrc = SQLConnect(hdbc, (SQLCHAR*)dsn, SQL_NTS, (SQLCHAR*)uid, SQL_NTS, (SQLCHAR*)pwd, SQL_NTS);
    if (sqlrc != SQL_SUCCESS && sqlrc != SQL_SUCCESS_WITH_INFO) {
    printf("SQLConnect Error\n");
    return 9;
    }

    // statement handle
    sqlrc = SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt);
    if (sqlrc != SQL_SUCCESS && sqlrc != SQL_SUCCESS_WITH_INFO) {
    printf("SQLAllocHandle SQL_HANDLE_STMT Error\n");
    return 9;
    }

    // statement prepare
    sqlrc = SQLPrepare(hstmt, (SQLCHAR*)sql, SQL_NTS);
    if (sqlrc != SQL_SUCCESS && sqlrc != SQL_SUCCESS_WITH_INFO) {
    printf("SQLPrepare Error\n");
    return 9;
    }

    // get result columns
    sqlrc = SQLNumResultCols(hstmt, &cols);
    if (sqlrc != SQL_SUCCESS && sqlrc != SQL_SUCCESS_WITH_INFO) {
    printf("SQLNumResultCols Error\n");
    return 9;
    }

    // execute
    sqlrc = SQLExecDirect(hstmt, (SQLCHAR*)sql, SQL_NTS);

    // display result in CSV format
    while (TRUE) {
    sqlrc = SQLFetch(hstmt);

    if (sqlrc == SQL_SUCCESS || sqlrc == SQL_SUCCESS_WITH_INFO) {
    for (i = 1; i < cols+1; i++) {
    if (i > 1) {printf(",");}
    strcpy(svalue, "");
    SQLGetData(hstmt, i, SQL_C_CHAR, (SQLCHAR*)svalue, 2048, &inum);
    printf(svalue);
    }
    printf("\n");
    } else {
    if (sqlrc == SQL_NO_DATA) {
    printf("\n");
    } else {
    // otherwise error
    printf("SQLFetch ERROR\n");
    }
    break;
    }
    }
    }

Similar Threads

  1. How to connect PHP to mySQL Database
    By Nathen in forum Tips & Tweaks
    Replies: 2
    Last Post: 17-07-2011, 03:49 AM
  2. How to connect ASP to different database
    By Elijah2010 in forum Software Development
    Replies: 5
    Last Post: 16-02-2010, 12:01 AM
  3. how to connect VB6 to database
    By WILTON in forum Software Development
    Replies: 3
    Last Post: 08-12-2009, 04:26 AM
  4. How to Connect Database and Retrieve it with the use of C#?
    By CheckMeNot in forum Software Development
    Replies: 3
    Last Post: 19-11-2009, 09:56 AM
  5. How to Connect a database (MDB) to excel
    By Rebeccak in forum Tips & Tweaks
    Replies: 0
    Last Post: 11-02-2009, 11:46 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,051,485.70758 seconds with 17 queries