
into语句。具体格式:
insert
into
表名(列名1,列名2,...,列名n)values('值1','值2',...,'值n')
C#连接数据库有以下几个步骤:1:使用配置的数据库连接串,创建数据库连接 Connection 对象
2:构建 *** 作的sql语句
3:定义command对象
4:打开数据连接
5:执行命令
举一个例子,删除 *** 作
public class StudentService
{
//从配置文件中读取数据库连接字符串
private readonly static string connString = ConfigurationManager.ConnectionStrings["accpConnectionString"].ToString()
private readonly static string dboOwner = ConfigurationManager.ConnectionStrings["DataBaseOwner"].ToString()
AdoNetModels.Student model = new Student()
#region 删除数据1
public int DeleteStudent(int stuID)
{
int result = 0
// 数据库连接 Connection 对象
SqlConnection connection = new SqlConnection(connString)
// 构建删除的sql语句
string sql = string.Format("Delete From Student Where stuID={0}", stuID)
// 定义command对象
SqlCommand command = new SqlCommand(sql, connection)
try
{
connection.Open()
result = command.ExecuteNonQuery() // 执行命令
}
catch (Exception ex)
{
Console.WriteLine(ex.Message)
}
finally
{
connection.Close()
}
return result
}
#endregion
1.准备工作: 准备相关的软件(Eclipse除外,开源软件可以从官网下载) <1>.Microsoft SQL server 2005 Express Edition 下载地址:http://download.microsoft.com/download/0/9/0/09020fab-d2c3-4a8c-b9e0-db53a7a30ae8/SQLEXPR_CHS.EXE <2>.SQL Server Management Studio 下载地址:http://www.microsoft.com/downloads/details.aspx?displaylang=zh-cn&FamilyID=c243a5ae-4bd1-4e3d-94b8-5a0f62bf7796#filelist <3>.SQL Server 2005 driver for JDBC 下载地址:http://download.microsoft.com/download/8/B/D/8BDABAE2-B6EA-41D4-B903-7916EF3690EF/sqljdbc_1.2.2323.101_enu.exe 2.JDBC连接SQL Server的驱动安装 ,前两个是属于数据库软件,正常安装即可(注意数据库登陆不要使用windows验证) 是用java连接吗? 如果是,方法如下: <1>将JDBC解压缩到任意位置,比如解压到C盘program files下面,并在安装目录里找到sqljdbc.jar文件,得到其路径开始配置环境变量 在环境变量classpath 后面追加 C:\Program Files\Microsoft SQL Server2005 JDBC Driver\sqljdbc_1.2\enu\sqljdbc.jar <2>设置SQLEXPRESS服务器: a.打开SQL Server Configuration Manager ->SQLEXPRESS的协议 ->TCP/IP b.右键单击启动TCP/IP c.双击进入属性,把IP地址中的IP all中的TCP端口设置为1433 d.重新启动SQL Server 2005服务中的SQLEXPRESS服务器 e.关闭SQL Server Configuration Manager <3>打开刚刚安装好的 SQL Server Management Studio,连接SQLEXPRESS服务器, 新建数据库,起名字为sample <4>打开Eclipse a.新建工程->Java ->Java project,起名为Test b.选择eclipse->窗口->首选项->Java->installed JRE 编辑已经安装好的jdk,查找目录添加sqljdbc.jar c.右键单击目录窗口中的Test, 选择Build Path ->Configure Build Path..., 添加扩展jar文件,即把sqljdbc.jar添加到其中 <5>编写Java代码来测试JDBC连接SQL Server数据库欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)