使用.NET中的一个数据库 [英] Using a database in .NET

查看:172
本文介绍了使用.NET中的一个数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

已更新问题:



好吧,我要简化我的问题,因为我真的不知道该怎么回答你的问题。



说我创建一个新的Windows窗体应用程序,
,则项目 - >添加新用品 - >本地数据库。



然后在数据库资源管理器创建一个表(TestTable的),并给它一个ID列和值列。



可以为我提供的步骤简单地添加从代码一行到数据库



老问题:




我一直在努力做一些事情
,我认为会很容易,但
从来没有使用C#和之前我
有问题的细节。我
简单的想要使用
的Visual C#Express 2008的SQL数据库。



有关测试的目的,我有一个DataGrid
我。形式,可以反映出修改
分贝



如果我用这个:

  codesTableAdapter.Fill(dataSet1.codes); 



数据网格(数据集)将与
从SQL数据库中的信息填写。



如果我那么做这样的事情:

  codesTableAdapter.InsertQuery(txtCode 。文本,txtName.Text); 
codesTableAdapter.Fill(dataSet1.codes);
codesTableAdapter.Update(dataSet1的);
dataSet1.AcceptChanges();



数据网格反映了变化,但
如果关闭程序并转到
数据库的更改是不存在的。
当我打开程序再次
的变化是不存在的。



我有一种感觉,这是不是太清楚
作为在这里我的理解是非常低的
,所以请让我知道什么是需要其他信息



解决方案

真的很难明白是怎么回事,没有看到你的代码,但我会做出一些猜测:




说我创建一个新的Windows窗体应用程序,然后项目 - >添加新用品 - >本地数据库


这将在项目中创建一个空的SQL Server精简版.sdf文件。您可以编辑从Visual Studio或SQL CE查询分析器此文件。




然后在数据库资源管理器中创建一个表(TestTable的),并给它一个ID栏和VALUE栏。


这会在你的.sdf文件创建一个空表。这些都是很糟糕的名字,但对于测试的目的,我们可以使用它们。




你能为我提供的步骤只是一个排从添加到数据库代码?


System.Data.SqlServerCe 提供方法来管理.SDF文件。要添加一行到您的测试表:

 使用(的SqlCeConnection MyConnection的=新的SqlCeConnection(@数据源= Path\To \Your\SDF\file.sdf;采用))
(SqlCeCommand myCmd = myConnection.CreateCommand())
{
myCmd.CommandType = CommandType.Text;
myCmd.CommandText =INSERT INTO TestTable的(ID,[VALUE])VALUES(1,等等);
myConnection.Open();
myCmd.ExecuteNonQuery();
}



请注意, Path\To\Your\SDF\\ \\file.sdf 可能不是你的想; Visual Studio的默认复制项目文件到项目的构建输出路径(例如Path\to\your\project\bin\Debug\file.sdf)。如果更改则内置文件重建项目,原来.SDF文件被重新复制到你的输出路径,消灭您对您的项目文件所做的任何更改。如果这不是你想要的,你将不得不解释你正在试图完成什么。



例如,要更改仅适用于内置的项目文件,更改数据文件复制到输出目录,在VS属性为复制如果较新的。


UPDATED QUESTION:

Ok, I am going to simplify my question since I don't really know how to answer your questions.

Say I create a new Windows Forms Application, then Project->Add New Item->Local Database.

Then in Database Explorer I create a table ("testtable") and give it an "ID" column and "VALUE" column.

Can you provide me with the steps to simply add a row to the database from the code?

OLD QUESTION:

I have been trying to do something that I think would be really easy but have never used C# before and am having trouble with the details. I simple want to use a sql database with Visual C# Express 2008.

For testing purposes I have a datagrid on my form that can reflect changes to the db.

If i use this:

codesTableAdapter.Fill(dataSet1.codes);

The datagrid(dataset) will fill with the info from the sql database.

If i then do something like this:

codesTableAdapter.InsertQuery(txtCode.Text,txtName.Text);
codesTableAdapter.Fill(dataSet1.codes);
codesTableAdapter.Update(dataSet1);
dataSet1.AcceptChanges();

The datagrid reflects the changes but if close the program and go to the database the changes are not there. When I open the program again the changes are not there.

I have a feeling this isn't too clear as my understanding here is very low so please let me know what other info is needed.

解决方案

Really hard to understand what is going on without seeing your code, but I will make some guesses:

Say I create a new Windows Forms application then Project->Add New Item->Local Database

This will create an empty SQL Server Compact Edition .SDF file in your project. You can edit this file from Visual Studio or SQL CE Query Analyzer.

Then in Database Explorer I create a table ("testtable") and give it an "ID" column and "VALUE" column.

This will create an empty table in your .SDF file. Those are pretty bad names, but for test purpose we can use them.

Can you provide me with the steps to simply add a row to the database from the code?

System.Data.SqlServerCe provides methods to manage .SDF files. To add a row to your test table:

using (SqlCeConnection myConnection = new SqlCeConnection(@"Data Source=Path\To\Your\SDF\file.sdf;"))
using (SqlCeCommand myCmd = myConnection.CreateCommand())
{
    myCmd.CommandType = CommandType.Text;
    myCmd.CommandText = "INSERT INTO testtable (ID, [VALUE]) VALUES (1, whatever)";
    myConnection.Open();
    myCmd.ExecuteNonQuery();
}

Be aware that Path\To\Your\SDF\file.sdf may not be what your think; Visual Studio by default copies project files to the Project Build Output Path (e.g. Path\to\your\project\bin\Debug\file.sdf). If you change the built file then rebuild your project, the original .SDF file gets recopied to your output path, wiping out any changes you have made to your project file. If that's not what you want you will have to explain what you are trying to accomplish.

For example, to make changes apply only to the built project file, change your data file "Copy to Output Directory" property in VS to "Copy if Newer".

这篇关于使用.NET中的一个数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆