1

我有一个主表单名称调用 fTest.cs

在这个主要我有按钮将数据保存到 sql server 和 sqlite

这是我的代码

private void btnSave_Click(object sender, EventArgs e)
        {
            Dictionary<String, String> dataSqlite = new Dictionary<String, String>();
            Dictionary<String, String> dataSqlserv = new Dictionary<String, String>();
            dataSqlite.Add("name", "puja");
            dataSqlite.Add("score", "3232");
            dataSqlserv.Add("ID", "32");
            dataSqlserv.Add("Name", "puja lagi");


             cDatabaseSQLite.Insert("highscore", dataSqlserv);
            cDatabaseSQLite.Insert("highscore", dataSqlserv);
            cDatabaseSQLite.Insert("highscore", dataSqlserv);

            cDatabaseSQLServer.Insert("tblTest", dataSqlite);
            cDatabaseSQLServer.Insert("tblTest", dataSqlite);
        }

我在 cDatabases.cs 中调用 insert 方法

public bool Insert(String tableName, Dictionary<String, String> data)
        {
            switch (sqlType)
            {
                case DATABASE_SQL_TYPE.DATABASE_SQL_TYPE_SQLITE:
                    return cSQLite.Insert(tableName, data);
                case DATABASE_SQL_TYPE.DATABASE_SQL_TYPE_MSSQL:
                    return cSQL.Insert(tableName, data);
            }
            return false;
        }

这是插入到我的 sqlite 数据库中

public bool Insert(String tableName, Dictionary<String, String> data)
        {
            String columns = "";
            String values = "";
            Boolean returnCode = true;
            foreach (KeyValuePair<String, String> val in data)
            {
                columns += String.Format(" {0},", val.Key.ToString());
                values += String.Format(" '{0}',", val.Value);
            }
            columns = columns.Substring(0, columns.Length - 1);
            values = values.Substring(0, values.Length - 1);
            try
            {
                this.ExecuteNonQuery(String.Format("insert into {0}({1}) values({2});", tableName, columns, values));
            }
            catch (Exception fail)
            {
                MessageBox.Show(fail.Message);
                returnCode = false;
            }
            return returnCode;
        }

这是 sql server 插入数据库

public bool Insert(String tableName, Dictionary<String, String> data)
        {
            String columns = "";
            String values = "";
            Boolean returnCode = true;
            foreach (KeyValuePair<String, String> val in data)
            {
                columns += String.Format(" {0},", val.Key.ToString());
                values += String.Format(" '{0}',", val.Value);
            }
            columns = columns.Substring(0, columns.Length - 1);
            values = values.Substring(0, values.Length - 1);
            try
            {
                this.ExecuteNonQuery(String.Format("insert into {0}({1}) values({2});", tableName, columns, values));
            }
            catch (Exception fail)
            {
                MessageBox.Show(fail.Message);
                returnCode = false;
            }
            return returnCode;
        }

当我尝试调试并按下按钮保存数据时无法保存到 sqlite 和服务器,或者数据为空。如何插入数据?

4

0 回答 0