Results 1 to 7 of 7
  1. #1

    Default SQL + C# connection


    uhmm mga bro ask lang unta ko unsaon ug connect sa ako program sa ako sql file....la pa ni discuss sa amo pero advance study ra ko...unsaon diay ni siya mga bro na mag insert ko data sa ako table using c# codes? tnx really appreciate if naay mo reply...

  2. #2
    Quote Originally Posted by Thelastairbender View Post
    uhmm mga bro ask lang unta ko unsaon ug connect sa ako program sa ako sql file....la pa ni discuss sa amo pero advance study ra ko...unsaon diay ni siya mga bro na mag insert ko data sa ako table using c# codes? tnx really appreciate if naay mo reply...
    What sql server are you using? MS Sql, Mysql, Oracle, Postgre, etc?

  3. #3
    microsoft server 2005 amo gamit sir

  4. #4
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Data;
    using System.Data.SqlClient;
    namespace ConsoleApplication1
    {
    class Program
    {
    static SqlConnection conn;
    static void Main(string[] args)
    {
    string strConn = @"Server =(local); database=TestDB; uid= sa; pwd =pass19251950";

    try
    {
    conn = new SqlConnection(strConn);
    conn.Open();
    Console.WriteLine("Successfully open........");

    }
    catch (SqlException e)
    {
    Console.WriteLine(e.Message);

    }
    finally
    {
    if (conn.State == ConnectionState.Open)
    {
    conn.Close();
    Console.WriteLine("Successfully close........");

    }
    }
    Console.ReadLine();

    }
    }
    }

  5. #5
    Quote Originally Posted by Thelastairbender View Post
    microsoft server 2005 amo gamit sir
    You use khiphistos codes for connecting to the db server.

    For data manipulation you can have the basics

    Insert record
    ...
    SqlCommand command =
    new SqlCommand("INSERT INTO table1 " +
    "VALUES('Test','Test',123,'blaah');",
    conn);
    command.ExecuteNonQuery();

    Console.WriteLine("A new record has been created.");
    ...

    Retrieving data

    ...
    SqlCommand command =
    new SqlCommand("SELECT * FROM table1",conn);
    SqlDataReader reader = command.ExecuteReader();
    while(reader.Read()){
    Console.WriteLine(reader.GetString(0));
    ....
    ....
    }
    reader.Close();
    Console.WriteLine("End of program.");
    ...

  6. #6
    conn;<----is this a variable?
    and whats reader?
    reader.Close();
    Last edited by Deathnote; 08-01-2009 at 09:46 AM.

  7. #7
    Quote Originally Posted by Deathnote View Post
    conn;<----is this a variable?
    and whats reader?
    reader.Close();
    hehehe...
    I presume that the object conn is the taken from khiphistos code which is the SqlConnection object..

    reader object is the SqlDataReader which is used to traverse the data return by SqlCommand....basically command.ExecuteReader() method call. If you inspect ExecuteReader() method of SqlCommand it returns SqlDataReader object.

    reader.Close() - its always safe to close open data reader when its not in use anymore.

  8.    Advertisement

Similar Threads

 
  1. Faxing from PC while connected online through DSL?
    By warfab_anime in forum Computer Hardware
    Replies: 9
    Last Post: 03-01-2013, 03:10 PM
  2. SQL Server 2k5 Remote connection error Part 2
    By hybrid_X in forum Programming
    Replies: 0
    Last Post: 06-02-2007, 05:09 PM
  3. SQL Server 2k5 Remote connection error
    By hybrid_X in forum Programming
    Replies: 1
    Last Post: 06-01-2007, 09:11 PM
  4. How to connect to SQL SERVER in Visual Basic.NET?
    By kibotizer in forum Programming
    Replies: 0
    Last Post: 03-16-2007, 11:55 PM
  5. HOW TO CONNECT SQL SERVER USING SQL AUTHENTICATION
    By edshark in forum Software & Games (Old)
    Replies: 13
    Last Post: 09-02-2005, 04:53 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
about us
We are the first Cebu Online Media.

iSTORYA.NET is Cebu's Biggest, Southern Philippines' Most Active, and the Philippines' Strongest Online Community!
follow us
#top