C# ADO.net

//仅仅记录原理方便查阅   代码质量较次
先得到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);

        }

你可能感兴趣的:(C++,c,mysql,.net,C#)