将记录保存到数据库 [英] saving records to database

查看:106
本文介绍了将记录保存到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在数据库中保存新数据?

How to save new data in a database?

推荐答案

假设您正在使用某种形式的SQL语法,则需要INSERT语句:
Assuming you are using SQL syntax of some form, you need an INSERT statement:
INSERT INTO myTable (myColumnName1, myColumnName2) VALUES ('Text for column1', 17)


如果使用.NET语言执行此操作,则:
1)使用连接字符串创建SqlConnection的实例
2)使用SqlConnection.Open方法打开连接
3)使用上面的SQL字符串创建SqlCommand的实例,并将其连接到刚打开的SqlConnection.
4)您在SqlCommand上执行方法ExecuteNonQuery.

还有其他方法,也有一些改进,例如使用参数化查询等处理各种对象,等等,但是它们可以等到您掌握了基础知识之后.


If you are doing this in a .NET language then:
1) You create an instance of an SqlConnection, using your connection string
2) Open the connection using the SqlConnection.Open method
3) You create an instance of an SqlCommand, using the above SQL string, and connecting it to the SqlConnection you just opened.
4) You execute the method ExecuteNonQuery on the SqlCommand.

There are other ways, and there are refinements such as Disposing of the various objects, using Parametrized queries and so forth, but they can wait until you have the basics working.


这篇关于将记录保存到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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