本文介绍了在.sdf生活中插入数据的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尊敬的专家,

我是C#的新手.我的要求是动态创建数据库文件(在本地系统中),做类似设置和操作的操作得到.我能够动态创建.sdf文件,并且能够成功读取.但是在将数据插入.sdf文件时,其无法正常工作.

我创建.sdf文件的方式是
通过sloution Explorer添加-> NewItem-> Data-> LocalDatabase.

下面,我更新了我的代码(创建了4个基于控制台的应用程序以进行测试).请让我知道代码中的错误.



Dear Expert,

I''m new to C#. My requirement is to create a DB file (in local system) dynamically & do operation like set & get. I''m able to create .sdf file dynamically and successfully able to read. but while inserting data in .sdf file, its not working properly.

The way i created .sdf file is
Add->NewItem->Data->LocalDatabase through sloution explorer.

Below I''ve updated my code (created 4 console based app for testing purpose). Please let me know what is the wrong in the code.



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.SqlServerCe;
using System.Data.SqlClient;

namespace ConsoleApplication2
{
    class Program
    {
        static public void ReadData()
        {
            string connStr = "Data Source = TempDB.sdf; Password =";

            //if (!File.Exists("DBName.sdf"))
            {
                SqlCeConnection conn = new SqlCeConnection(connStr);
                try
                {
                    SqlCeEngine engine = new SqlCeEngine(connStr);
                   
                    conn.Open();

                    SqlCeCommand cmd = conn.CreateCommand();
                    
                    cmd.CommandText = "SELECT * FROM TestTable WHERE Name = 'Krishna'";
                    SqlCeDataReader myReader = cmd.ExecuteReader();

                    while (myReader.Read())
                    {
                        string strGroup = myReader.GetString(1);
                        
                        int ll = 0;
                        ll++;
                    }

                    

                }
                catch (SqlException ex)
                //catch(Exception)
                {
                    string strMessage = "\nFailed to Read ***";
                    strMessage += ex.Message;
                    // Log the exception
                    Console.WriteLine(strMessage);
                }
                finally
                {
                    conn.Close();
                }
            } 
        }

        static public void WriteData()
        {
            string connStr = "Data Source = TempDB.sdf;";

            //if (!File.Exists("DBName.sdf"))
            {
                SqlCeConnection conn = new SqlCeConnection(connStr);
                conn.Open();
                SqlCeCommand cmd = new SqlCeCommand("INSERT INTO TestTable ([Name], [Group]) Values('Krishna', 'D1')", conn);
                try
                {
                    int iRes = cmd.ExecuteNonQuery();
                    int t = 0;
                    t++;
                }
                catch (Exception)
                {
                    Console.WriteLine("\nFailed to Write\n");
                }
                conn.Close();
            }
        }

        static void Main(string[] args)
        {
            // Write data
            WriteData();

            // Read Data
            ReadData();
        }
    }
}




提前谢谢大家.




Thank you all in advance.

推荐答案


这篇关于在.sdf生活中插入数据的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 00:34