You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在Mac上使用GTK C#连接并编辑Microsoft Azure SQL数据库

可以用GTK#连接并编辑Azure SQL数据库表(无需.NET框架)

完全没问题!因为GTK#本身是基于Mono生态的,而Mono提供了适配跨平台的数据库访问组件,刚好能满足你在Mac 10.11.16环境下的需求。下面是具体的实现思路和步骤:

核心依赖:Mono.Data.SqlClient

你需要使用Mono.Data.SqlClient组件——这是Mono官方提供的、兼容SQL Server/Azure SQL的数据库驱动,和GTK#完美兼容,不需要依赖.NET框架。

步骤1:添加组件引用

在Visual Studio for Mac中:

  • 右键你的GTK#项目 → 选择「添加」→「引用」
  • 在弹出的窗口里找到Mono.Data.SqlClient,勾选后点击确定。

步骤2:构建Azure SQL连接字符串

Azure SQL的连接字符串需要包含服务器地址、认证信息和加密设置,格式如下(替换占位符为你的实际信息):

string connectionString = "Server=tcp:[你的Azure SQL服务器名].database.windows.net,1433;Initial Catalog=[你的数据库名];Persist Security Info=False;User ID=[你的登录用户名];Password=[你的登录密码];MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Connection Timeout=30;";

注意:Encrypt=True是必须的,Azure SQL强制要求加密连接。

步骤3:编写CRUD操作代码

下面是几个常用操作的示例,你可以直接集成到GTK#应用中:

查询数据

using Mono.Data.SqlClient;
// ...

public void FetchDataFromAzureSQL()
{
    using (var conn = new SqlConnection(connectionString))
    {
        try
        {
            conn.Open();
            string query = "SELECT Id, Name, Description FROM YourTableName";
            
            using (var cmd = new SqlCommand(query, conn))
            using (var reader = cmd.ExecuteReader())
            {
                while (reader.Read())
                {
                    // 读取数据,比如绑定到GTK的TreeView或者ListStore
                    int id = reader.GetInt32(0);
                    string name = reader.GetString(1);
                    Console.WriteLine($"ID: {id}, Name: {name}");
                }
            }
        }
        catch (SqlException ex)
        {
            // 处理数据库异常,比如在GTK窗口显示错误提示
            Console.WriteLine($"数据库错误: {ex.Message}");
        }
    }
}

插入/更新/删除数据

一定要用参数化查询防止SQL注入,示例如下:

public int InsertNewRecord(string name, string description)
{
    using (var conn = new SqlConnection(connectionString))
    {
        try
        {
            conn.Open();
            string insertQuery = "INSERT INTO YourTableName (Name, Description) VALUES (@Name, @Description)";
            
            using (var cmd = new SqlCommand(insertQuery, conn))
            {
                cmd.Parameters.AddWithValue("@Name", name);
                cmd.Parameters.AddWithValue("@Description", description);
                return cmd.ExecuteNonQuery(); // 返回受影响的行数
            }
        }
        catch (SqlException ex)
        {
            Console.WriteLine($"插入失败: {ex.Message}");
            return -1;
        }
    }
}

关键注意事项

  • Azure SQL防火墙设置:必须在Azure门户中把你的Mac的公网IP添加到SQL服务器的防火墙允许列表里,否则会出现连接超时/拒绝访问的错误。
  • UI线程避免阻塞:数据库操作是耗时任务,不能直接在GTK的UI线程中执行,否则会导致界面卡死。建议用后台线程或Task处理,再通过Application.Invoke更新UI:
// 示例:GTK按钮点击事件
yourButton.Clicked += (sender, e) =>
{
    Task.Run(() =>
    {
        // 执行数据库操作
        int result = InsertNewRecord("测试名称", "测试描述");
        
        // 回到UI线程更新界面
        Gtk.Application.Invoke(delegate
        {
            yourStatusLabel.Text = result > 0 ? "插入成功" : "插入失败";
        });
    });
};
  • Mono版本兼容性:Mac 10.11.16比较老旧,确保你使用的Visual Studio for Mac版本自带的Mono支持Mono.Data.SqlClient——一般来说,当时的Visual Studio for Mac版本(比如7.x系列)自带的Mono是兼容的,如果遇到问题可以尝试安装适配OS X 10.11的Mono版本。

内容的提问来源于stack exchange,提问作者user3882865

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 09:35:40