Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 使用C从服务器读取数据库名称#_C#_Mysql_Database - Fatal编程技术网

C# 使用C从服务器读取数据库名称#

C# 使用C从服务器读取数据库名称#,c#,mysql,database,C#,Mysql,Database,基本上,我正在创建一个程序,允许用户从服务器读取多个数据库名称。我已经将服务器连接到该程序,连接工作正常(为了安全起见,一些细节被替换为*)。但是,我似乎无法使数据库名称显示在数据网格视图上。救命啊 这是我迄今为止的工作 private void connectDB_Click(object sender, EventArgs e) { try { using (SqlConnection connection = new SqlC

基本上,我正在创建一个程序,允许用户从服务器读取多个数据库名称。我已经将服务器连接到该程序,连接工作正常(为了安全起见,一些细节被替换为*)。但是,我似乎无法使数据库名称显示在数据网格视图上。救命啊

这是我迄今为止的工作

private void connectDB_Click(object sender, EventArgs e)
    {
        try
        {
            using (SqlConnection connection = new SqlConnection(@"Data Source=***;Integrated Security=False;User ID=***;Password=***;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False"))
            using (SqlCommand command = new SqlCommand("Select * FROM sys.databases;"))
            {
                connection.Open();
                using (SqlDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        foreach(SqlDataReader read in reader)
                        {
                        int n = dataGridDataBase.Rows.Add();
                        dataGridDataBase.Rows[n].Cells[0].Value = reader;
                        }
                    }

                }
            }

        }
        catch (Exception ex)
        {
            throw ex;
        }
    }

请注意,SQLCommand对象未与SQLConnection链接

using (SqlConnection connection = new SqlConnection(@"Data Source=***;Integrated Security=False;User ID=***;Password=***;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False"))
using (SqlCommand command = new SqlCommand("Select * FROM sys.databases;"))
{
      command.Connection = connection;//This is missing in your code.
      connection.Open();

      //Bind your datagridview to rows from the reader        
}

.DataBind()文件;这对我不起作用。出现一个错误,说明没有“数据绑定的定义”。请删除该行代码。我假设它是ASP.Net。如果是WinForms,则DataGridView没有DataBind()方法。好的,我已删除它。然而,数据网格上仍然没有显示任何内容。如何让它显示服务器中数据库的名称?