本文介绍了SQL查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有命令对SQL数据库进行查询?

我设法创建一个sql连接变量,并从中获取信息

带有select查询的SqlDataAdapter,并用它填充

数据集。

如果我想进行插入查询,我不能,因为它没有保存在数据库中,当我关闭应用程序时,我无法再访问它了。

这就是我所做的:


SqlConnection cn = new SqlConnection();

SqlDataAdapter da;


cn = table1TableAdapter.Connection;

cn .Open();

da = new SqlDataAdapter(" INSERT INTO Table1(item,title,cost)

VALUES(''milk'',''低脂肪'',''100'')",cn);

da.InsertCommand = da.SelectCommand;

da.Fill(myDBds," Items") ;

cn.Close();

如何查询?

请帮助,

Ofir。

Is there a command to do a query for an SQL database?
I managed to make a sql connection variable, and to take info from it
by making a SqlDataAdapter with select query in it, and filling the
dataset with it.
If I want to do an insert query, I can''t, because it is not saved in
the DB, and when I close the application I can''t access it any more.
This is what I did:

SqlConnection cn = new SqlConnection();
SqlDataAdapter da;

cn = table1TableAdapter.Connection;
cn.Open();
da = new SqlDataAdapter("INSERT INTO Table1(item,title,cost)
VALUES(''milk'',''low fat'',''100'')",cn);
da.InsertCommand = da.SelectCommand;
da.Fill(myDBds, "Items");
cn.Close();
How do I query?
Please help,
Ofir.

推荐答案



SqlConnection cn = new SqlConnection();

SqlCommand cmd;


cn = table1TableAdapter .Connection;

cn.Open();

cmd = new SqlCommand(" INSERT INTO Table1(item,title,cost)

VALUES(''milk'',''low fat'',''100'')",cn);

cmd.ExecuteNonQuery();

cmd .Dispose();

cn.Close();

SqlConnection cn = new SqlConnection();
SqlCommand cmd;

cn = table1TableAdapter.Connection;
cn.Open();
cmd = new SqlCommand("INSERT INTO Table1(item,title,cost)
VALUES(''milk'',''low fat'',''100'')",cn);
cmd.ExecuteNonQuery();
cmd.Dispose();
cn.Close();




另一个回复解释了如何做,但重要的是

注意插入*不是* a查询 - 它正在添加数据,而不是

阅读它。


Jon

The other reply explained how to do it, but the important thing to
note is that an insertion *isn''t* a query - it''s adding data, not
reading it.

Jon




SqlConnection cn = new SqlConnection();

SqlCommand cmd;


cn = table1TableAdapter .Connection;

cn.Open();

cmd = new SqlCommand(" INSERT INTO Table1(item,title,cost)

VALUES(''milk'',''low fat'',''100'')",cn);

cmd.ExecuteNonQuery();

cmd .Dispose();

cn.Close();


SqlConnection cn = new SqlConnection();
SqlCommand cmd;

cn = table1TableAdapter.Connection;
cn.Open();
cmd = new SqlCommand("INSERT INTO Table1(item,title,cost)
VALUES(''milk'',''low fat'',''100'')",cn);
cmd.ExecuteNonQuery();
cmd.Dispose();
cn.Close();



但是,如果我关闭应用程序,仍然没有牛奶。项目

了。

为什么DB没有更改?

请帮助,

Ofir。

Thenks, but still if I close the aplication, There is no "milk" item
any more.
Why the DB is not changed?
Please help,
Ofir.


这篇关于SQL查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 18:40