如何在C#中连接表(SQL DATABASE) [英] how to connect tables (SQL DATABASE) in C#

查看:86
本文介绍了如何在C#中连接表(SQL DATABASE)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我在学校有一个项目,我在用c#连接表时遇到麻烦,并且在SQL中有数据库.谁能帮我 ?任何帮助都会很棒.

情况是这样的:
我有两个桌子.
第一个具有带有主键的字段,第二个仅具有不带键的字段.

如何建立两个表之间的关系?

最好的问候!

Hi everyone,

I have a project in school , and I''m having a trouble to connect tables in c#, and I have a database in SQL. Can anyone help me ? Any help would be great.

The situation is like this:
I''ve got two tables.
First got a field with the primary key, and the second has just the field without key.

How to make the relationship between two tables?!

Best regards !

推荐答案

您好
解决方案并不像您想象的那么简单.

首先,您需要从C#程序连接到数据库.
要从C#.NET连接到MS SQL Server数据库,您需要创建如下的连接字符串:

私有SqlConnection连接;
私有字符串connectionString =
@服务器=(本地);数据库= Embedding_SQL_Test;用户ID = sa;密码= 123";
connection =新的SqlConnection(connectionString);

接下来,使用上面创建的SqlConnection对象创建"SqlCommand",如下所示:
SqlCommand cmd =新的SqlCommand(从Customer中选择* *,其中CustomerID = @Cid",连接);

此处显示的SQL查询可以由SELECT,INSERT,UPDATE查询等代替.

接下来要在数据库中执行SQL查询,请使用以下方法:
ExecuteReader-执行SELECT查询
ExecuteNonQuery-执行INSERT,DELETE,UPDATE和SET语句.

这是关于如何从C#连接到SQL Server数据库以及如何在数据库中执行SQL查询的简短描述.
有关连接字符串,方法及其参数的详细信息,请检查以下链接:(
Hi
the solution is not as simple as you might think it is.

First of all you need to connect to the database from your C# program.
To connect to MS SQL Server database from C#.NET, you need to create a connection string such as below:

private SqlConnection connection;
private string connectionString =
@"Server=(local);Database=Embedding_SQL_Test;User ID=sa;Password=123";
connection = new SqlConnection( connectionString );

Next, you use the SqlConnection object created above to create a ''SqlCommand'', as shown below:
SqlCommand cmd = new SqlCommand( "select * from Customer where CustomerID = @Cid", connection);

The SQL query shown here can be replaced by a SELECT, INSERT, UPDATE queries etc.

Next to execute the SQL queries in the database, you use the following methods:
ExecuteReader - to execute SELECT queries
ExecuteNonQuery - to execute INSERT, DELETE, UPDATE, and SET statements.

This is a very short description of how to connect to SQL Server database from C# and execute SQL queries in the database.
For details about the connection string, the methods and their parameters check the following link: ( http://www.shahriarnk.com/Shahriar-N-K-Research-Embedding-SQL-in-C-Sharp-Java.html )
Here you will also find details about how to pass parameters to the SQL queries as well as calling stored procedures and much more.


听起来像您需要了解外键 b>.看看这些知识库文章:
MSDN:外键约束 [ MSDN:创建和修改外键约束 [ MSDN:创建和修改表 [
Sounds like you need to know about Foreign Keys. Have a look at these knowledge-base articles:
MSDN: FOREIGN KEY Constraints[^]
MSDN: Creating and Modifying FOREIGN KEY Constraints[^]

For more details: MSDN: Creating and Modifying Tables[^]


如果没有列在第二个表中,该表引用了第一个表中的键(称为外键),因此无法连接两个表.

无论您做什么,一个表中的主键值必须在第二个表中的列中递增,否则您就没有关系.
If there is no column in the second table that has a reference to the key in the first table (this is called a Foreign Key), you have no way of connecting the two tables.

Whatever you''re doing, the primary key value in one table must how up in a column in the second table, otherwise you have no relationship.


这篇关于如何在C#中连接表(SQL DATABASE)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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