3

これは今私のコードです:

private static MySqlConnection conn = null;
private static MySqlDataAdapter AccountsDa = null;
private static MySqlCommandBuilder AccountsCb = null;
AccountsDa = new MySqlDataAdapter("SELECT * FROM accounts", conn);
AccountsCb = new MySqlCommandBuilder(AccountsDa);
Accounts = new DataTable();
AccountsDa.Fill(Accounts);

次のようにすれば、手動で行う必要なく、列のデフォルト値を定義する方法を見つけようとしています。

DataColumn col = new DataColumn();
col.ColumnName = "id";
col.AllowDBNull = false;
col.DataType = System.Type.GetType("System.Int32");
col.DefaultValue = 0;
Accounts.Columns.Add(col);

すべての列で正常に動作しますが、テーブルがいっぱいになったときにデータベースからデフォルト値を自動的に設定するにはどうすればよいですか。手動で 30 列を定義する必要がないことを願っています。

Accountsda.FillSchema(Accounts, SchemaType.Source); を試しました。null と自動インクリメントを許可しますが、デフォルト値は設定しません

後でデータテーブルに行を追加するときに問題が発生します.1つの列の値を設定するだけで、残りの列はデフォルト値に頼る必要がある場合があります。

行を挿入するためのデフォルト値を手動で定義するために 180 行のコードを配置できますが、データ テーブルを作成/入力するときにデータベースからそれを取得する方法が必要です。

これは、オンライン rts ゲームの専用サーバー用であるため、データがたとえば 2 分間しか存在せず、その後再び削除される場合があるため、インメモリ データ テーブルを使用しています。データベースにヒットを保存するために、データテーブルを使用してそれらを操作し、10分ごとにフラッシュして、おそらく40,000ヒットではなく、10分ごとにデータベースに1,000ヒットしかないようにします

4

2 に答える 2

3

フォーラムで最終的に応答を得た後のmsdnの達人によると、デフォルト値を取得することはできません。実行できるのは、値がnullであるかどうか、およびその自動増分が許可されているかどうかをロードすることだけですが、シードを設定して自動増分をステップする必要があります。データベースからも取得されませんが、短縮バージョンが提供されます。 180行ではなく30行のコードになります

fillschemaを呼び出してからデータテーブルにデータを入力すると、このように簡単に実行でき、6行ではなく1行に削減されます。

Cities.Columns["wood"].DefaultValue = 0;

数回の返信の後、私が望んでいた方法ではなく、これを行うためのはるかに簡単な方法がありますが、列ごとに1行ではなく、同じ道を進んでいる他の人を助けるかもしれません。

foreach (DataColumn col in Cities.Columns) {
    if (col.ColumnName != "id") col.DefaultValue = 0;
}

idは主キーであり、デフォルト値を設定できません

于 2013-03-23T04:16:34.727 に答える
2

So I was trying to do something similar to you (except I have no idea how to get the information about auto increment) - I got the idea from https://stackoverflow.com/a/12731310/222897

    private void AssignMandatoryColumns([NotNull] DataTable structure, string tableName)
    {
        // find schema
        string[] restrictions = new string[4]; // Catalog, Owner, Table, Column
        restrictions[2] = tableName;
        DataTable schemaTable = _dbCon.GetSchema("Columns", restrictions);
        if (schemaTable == null) return;

        // set values for columns
        foreach (DataRow row in schemaTable.Rows)
        {
            string columnName = row["COLUMN_NAME"].ToString();
            if (!structure.Columns.Contains(columnName)) continue;
            if (row["IS_NULLABLE"].ToString() == "NO") structure.Columns[columnName].AllowDBNull = false;

            //if (structure.Columns[columnName].AutoIncrement) continue; // there can be no default value
            var valueType = row["DATA_TYPE"];
            var defaultValue = row["COLUMN_DEFAULT"];
            try
            {
                structure.Columns[columnName].DefaultValue = defaultValue;
                if (!structure.Columns[columnName].AllowDBNull && structure.Columns[columnName].DefaultValue is DBNull)
                {
                    Logger.DebugLog("Database column {0} is not allowed to be null, yet there is no default value.", columnName);
                }
            }
            catch (Exception exception)
            {
                if (structure.Columns[columnName].AllowDBNull) continue; // defaultvalue is irrelevant since value is allowed to be null
                Logger.LogWithoutTrace(exception, string.Format("Setting DefaultValue for {0} of type {1} {4} to {2} ({3}).", columnName, valueType, defaultValue, defaultValue.GetType(), structure.Columns[columnName].AllowDBNull ? "NULL" : "NOT NULL"));
            }
        }
    }

The function takes the DataTable you want to set the values for (I get mine by querying the DB) and the name of the table.

For some reason the timestamp and date columns don't like their default value no matter what I do.

于 2015-08-31T13:01:33.880 に答える