· 

Visual C# 点字⇔墨字変換 データベース版 II

 

今回対応した二重カギ[『』]、二重カッコ[《》]の内部では、いずれも前に外字符[5と6の点]があり、それと区別するのは機械的には矛盾があります。

 

そこで、私はそういう状態では仮名なのか英字なのかを区別する特殊入力方法を取ることにしました。

 

仮名の場合は[和]、英字の場合は[英]、英字二重大文字の場合は[大]と点字変換画面で入力してもらうことで解決しました。

 

点字のデータベースにはこれらの文字は入りません。

墨字変換の場合は、逆のことをして点字データの内容から墨字の表現として[和]、[英]、[大]を内部で作成して表示しています。

 

この方法を思いつくのに3日くらいかかりました。それを実現するためのロジックの作成にも何日か必要としました。

 

「T・エディタ」ではどの様な方法で入力を制御しているのか知りたい程です。

 

あとは、画面の色ですがダーク仕様の Windows11 を真似て濃いグレーを基準色にしました。

 

他には、点字変換画面で点字の表示領域に枠などを表示するかしないかを選択できるようにしました。純粋に点字だけを見たいときは表示しないを選びます。


利用するテーブルの種類と主キー、項目名

CLASS1 点字テキストデータベースへのアクセス方法変更

dataGridView をパラメータで渡すのをやめました。

ドキュメント名、ページ番号、点字テキストのそれぞれの List を OUT パラメータで受け渡す方法に変更しました。

こうすることで、 dataGridView が必要ない画面 Form では無理に作らなくて済むようになりました。

using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Data.SqlClient;

using System.Drawing;

using System.Linq;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;

 

namespace WindowsFormApplication1

{

    class DBText

    {

        public static bool ReadTextDB_All(out List<string> l_docu, out List<string> l_pgno, out List<string> l_text , string CS)

        {

            l_docu = new List<string>();

            l_pgno = new List<string>();

            l_text = new List<string>();

            bool ans = false;

            StringBuilder query = new StringBuilder();

            query.AppendLine("SELECT");

            query.AppendLine("    DocumentName");

            query.AppendLine("   ,PageNo");

            query.AppendLine("   ,PageText");

            query.AppendLine("FROM");

            query.AppendLine("    Tenji_Text");

            // 接続文字列の取得

            var connectionString = CS;

            using (var connection = new SqlConnection(connectionString))

            using (var command = connection.CreateCommand())

            {

                try

                {

                    // データベースの接続開始

                    connection.Open();

                    // SQLの設定

                    command.CommandText = query.ToString();

                    // SQLの実行

                    var adapter = new SqlDataAdapter(command);

                    using (SqlDataReader reader = command.ExecuteReader())

                    {

                        while (reader.Read())

                        {

                            ans = true;

                            l_docu.Add(reader["DocumentName"].ToString());

                            l_pgno.Add(reader["PageNo"].ToString());

                            l_text.Add(reader["PageText"].ToString());

                        }

                    }

                }

                catch (Exception exception)

                {

                    MessageBox.Show(exception.Message);

                    throw;

                }

                finally

                {

                    // データベースの接続終了

                    connection.Close();

                }

            }

            return ans;

        }

 

        public static bool ReadTextDB_Select(out List<string> l_docu, out List<string> l_pgno, out List<string> l_text , string _doc, int _pg, string CS)

        {

            l_docu = new List<string>();

            l_pgno = new List<string>();

            l_text = new List<string>();

 

            bool ans = false;

            StringBuilder query = new StringBuilder();

 

            query.AppendLine("SELECT");

            query.AppendLine("    DocumentName");

            query.AppendLine("   ,PageNo");

            query.AppendLine("   ,PageText");

            query.AppendLine("FROM");

            query.AppendLine("    Tenji_Text");

            query.AppendLine("WHERE");

            query.AppendLine("     DocumentName = @Doc");

            query.AppendLine("AND  PageNo = @Pag");

 

            // 接続文字列の取得

            var connectionString = CS;

            using (var connection = new SqlConnection(connectionString))

            using (var command = connection.CreateCommand())

            {

                try

                {

                    // データベースの接続開始

                    connection.Open();

 

                    // SQLの設定

                    command.CommandText = query.ToString();

                    command.Parameters.Add(new SqlParameter("@Doc", _doc));

                    command.Parameters.Add(new SqlParameter("@Pag", _pg));

                    // SQLの実行

                    var adapter = new SqlDataAdapter(command);

                    using (SqlDataReader reader = command.ExecuteReader())

                    {

                        while (reader.Read())

                        {

                            ans = true;

                            l_docu.Add(reader["DocumentName"].ToString());

                            l_pgno.Add(reader["PageNo"].ToString());

                            l_text.Add(reader["PageText"].ToString());

                        }

                    }

                }

                catch (Exception exception)

                {

                    MessageBox.Show(exception.Message);

                    throw;

                }

                finally

                {

                    // データベースの接続終了

                    connection.Close();

                }

            }

            return ans;

        }

 

        public static bool UpdateTextDB(Database1Entities2 _ent, string _doc, int _pg, string _txt, string CS)

        {

            bool ans = false;

            StringBuilder query = new StringBuilder();

 

            query.AppendLine("UPDATE");

            query.AppendLine("      Tenji_Text");

            query.AppendLine("SET PageText = ");

            query.AppendLine("    @txt");

            query.AppendLine("WHERE");

            query.AppendLine("     DocumentName = @Doc");

            query.AppendLine("AND  PageNo = @Pag");

 

            // 接続文字列の取得

            var connectionString = CS;

            using (var connection = new SqlConnection(connectionString))

            using (var command = connection.CreateCommand())

            {

                try

                {

                    // データベースの接続開始

                    connection.Open();

 

                    // SQLの設定

                    command.CommandText = query.ToString();

                    command.Parameters.Add(new SqlParameter("@txt", _txt));

                    command.Parameters.Add(new SqlParameter("@Doc", _doc));

                    command.Parameters.Add(new SqlParameter("@Pag", _pg));

                    // SQLの実行

                    var adapter = new SqlDataAdapter(command);

                    command.ExecuteNonQuery();

                    _ent.SaveChanges();

                    ans = true;

                }

                catch (Exception exception)

                {

                    MessageBox.Show(exception.Message);

                    throw;

                }

                finally

                {

                    // データベースの接続終了

                    connection.Close();

                }

            }

            return ans;

 

 

        }

 

        public static bool InsertTextDB(Database1Entities2 _ent, string _doc, int _pg, string _txt, string CS)

        {

            bool ans = false;

 

            // 接続文字列の取得

            var connectionString = CS;

            using (var connection = new SqlConnection(connectionString))

            using (var command = connection.CreateCommand())

            {

                try

                {

                    // データベースの接続開始

                    connection.Open();

 

                    // SQLの準備

                    command.CommandText = @"INSERT INTO Tenji_Text (DocumentName, PageNo, PageText) VALUES (@Doc, @PgNo, @PgTxt)";

                    command.Parameters.Add(new SqlParameter("@Doc", _doc));

                    command.Parameters.Add(new SqlParameter("@PgNo", _pg));

                    command.Parameters.Add(new SqlParameter("@PgTxt", _txt));

 

                    // SQLの実行

                    command.ExecuteNonQuery();

                    _ent.SaveChanges();

                    ans = true;

                }

                catch (Exception exception)

                {

                    MessageBox.Show(exception.Message);

                    ans = false;

                    throw;

                }

                finally

                {

                    // データベースの接続終了

                    connection.Close();

                }

            }

            return ans;

        }

 

    }

}

 

呼び出し側の例

namespace WidowsFormApplication1

{

    public partial class Form1 : Form

    {

        #region 変数・定数

        Form2 f2;

        Form3 f3;

        const string CS = @"ConnectionStrings";

 

    // List Param

        List<string> l_docu = new List<string>() ;

        List<string> l_pgno = new List<string>() ;

        List<string> l_text = new List<string>() ;

        Database1Entities _ent = new Database1Entities();

        #endregion;

            .

            .

            .

        #region ReadAll

        private bool ReadAll()

        {

            //データグリッドビュー クリア

            dataGridView1.Rows.Clear();

            clear();

            first = 0;

            bool ans = false;

            ans = DBText.ReadTextDB_All(out l_docu, out l_pgno, out l_text , CS);

            if(ans)

            {

                int cnt = l_docu.Count;

                for (int i = 0; i < cnt; i++)

                {

                    dataGridView1.Rows.Add(l_docu[i], l_pgno[i], l_text[i]);

                }

            }

            dataGridView1.ClearSelection();

            return ans;

        }

        #endregion;