C# ADO.net

原文链接: https://my.oschina.net/u/2296689/blog/545775

先得到connection
调用command
使用 reader 或者别的读取数据
//该处使用的是mysql
static void Main(string[] args)
        {
            String source = "DRIVER={MySQL ODBC 3.51 Driver};server=localhost;port=3306;uid=root;pwd=jack;database=goods2;option=3";
            String sqlStr = "select * from user";
            Console.WriteLine("connecting database ...");
            OdbcConnection connection = new OdbcConnection(source);
            if (connection.State == System.Data.ConnectionState.Closed)
            {
                try
                {
                    connection.Open();
                    Console.WriteLine("connected");
                    OdbcCommand cmd = new OdbcCommand(sqlStr, connection);
                    OdbcDataReader reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        Console.WriteLine(reader["id"] + "\t" + reader["name"]);
                    }
                }
                catch (OdbcException e) { 
                    Console.WriteLine(e.StackTrace);
                }
            }
            System.Threading.Thread.Sleep(5000);

        }

转载于:https://my.oschina.net/u/2296689/blog/545775

你可能感兴趣的:(C# ADO.net)