在c#中将数据插入sqlite和sql server

时间:2013-06-20 05:02:50

标签: c# sql-server sqlite

我有一个主表单名称调用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 insert database

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和服务器,或者数据为空。如何插入数据?

0 个答案:

没有答案