从C#将GUID存储在MySQL中

从C#将GUID存储在MySQL中,第1张

从C#将GUID存储在MySQL中

弄清楚了。就是这样 …

数据库架构

CREATE TABLE `test` (     `id` BINARY(16) NOT NULL,          PRIMARY KEY  (`id`))

string connectionString = string.Format("Server={0};Database={1};Uid={2};pwd={3}", "server", "database", "user", "password");Guid orgId = Guid.NewGuid();Guid fromDb = Guid.Empty;using (MySqlConnection conn = new MySqlConnection(connectionString)){    conn.Open();    using (MySqlCommand cmd = new MySqlCommand("INSERT INTO test (id) VALUES (?id)", conn))    {        cmd.Parameters.Add("id", MySqlDbType.Binary).Value = orgId.ToByteArray();        cmd.ExecuteNonQuery();    }    using (MySqlCommand cmd = new MySqlCommand("SELECt id FROM test", conn))    {        using (MySqlDataReader r = cmd.ExecuteReader())        { r.Read(); fromDb = new Guid((byte[])r.GetValue(0));        }    }}


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/4933031.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-11-13
下一篇 2022-11-13

发表评论

登录后才能评论

评论列表(0条)

保存