1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

C#とさくらのレンタルサーバーのMySQLをつなぐDB

Last updated at Posted at 2020-10-02

C# + さくらのレンタルサーバー + MySQLをつなぐ

参考画像

s1.PNG

とりあえずコード

public static string getDB()
        {
            // さくらのホスト名
            string host = "iiiiii.sakura.ne.jp";
            // さくらユーザー名
            string user = "iiiiii";
            // パスワード
            string Spassword = "password"; 


            // SSH接続
            var sshClient = new SshClient(host, 22, user, Spassword);
            sshClient.Connect();

            uint dbPort = 3306;
            uint localPort = 3306;
            //データベースのデータベースの名前とユーザー名
            string dbname = "DBname";
            string dbuser = "DBuser";
            string dbpassword = "Dpassword" ;
            //mysql0000.dbのやつ
            string serverName = "mysql1018.db.sakura.ne.jp";

            //サーバーフォワーディング
            var sshForward = new ForwardedPortLocal("127.0.0.1", localPort, serverName, dbPort);
            sshClient.AddForwardedPort(sshForward);
            sshForward.Start();

            var connectStr = "server=" + "127.0.0.1" + ";";
            connectStr += "port=" + dbport + ";";
            connectStr += "user=" + DBuser + ";";
            connectStr += "password=" + Dpassword + ";";
            connectStr += "database=" + dbname + ";";
            connectStr += "Pooling = False";

            string table = "testtb";

            //ここにSQL文を書く
            var sql = "SELECT * FROM " + table;

            string res = null;

            //MySQLサーバー接続
            using (var connection = new MySqlConnection(connectStr))
            using (var com = new MySqlCommand(sql, connection))
            {
                connection.Open();

                var selectCmd = new MySqlCommand(sql, connection);

                // 実行
                MySqlDataReader selectResult = selectCmd.ExecuteReader();

                // MySqlDataReaderというクラスに結果が入っている
                // Read()を呼ぶことで次の行にアクセスする

                while (selectResult.Read())
                {
                    var name = selectResult.GetString("name");  // フィールド名でのアクセス
                    string data = selectResult.GetString(1);  // カラムインデックスでのアクセス
                    if(name != "")
                    {
                        //ここでいい感じに処理
                        res += String.Format("{0}${1}\n", name, data);
                    }
                }

                // 結果を閉じる
                selectResult.Close();
                connection.Close();
                sshForward.Stop();
                sshClient.Disconnect();
                return res;
            }
        }

となる。頑張ろう

参考URL

ほぼこちらの内容です
https://qiita.com/skuromaku/items/67e46962228fe3cc6d7c

まずはこちらを参考にTeratermで接続したほうがよいかも
https://www.out48.com/archives/2495/

1
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?