c访问sqlserver,c访问sqlserver数据库

怎样访问sqlserver数据库

以sqlserver2008R2为例。

创新互联是专业的邓州网站建设公司,邓州接单;提供成都网站建设、成都做网站,网页设计,网站设计,建网站,PHP网站建设等专业做网站服务;采用PHP框架,可快速的进行邓州网站开发网页制作和功能扩展;专业做搜索引擎喜爱的网站,专业的做网站团队,希望更多企业前来合作!

1、打开sql2008,使用windows身份登录

2、登录后,右键选择“属性”。左侧选择“安全性”,选中右侧的“SQL Server 和 Windows 身份验证模式”以启用混合登录模式

3、选择“连接”,勾选“允许远程连接此服务器”,然后点“确定”

4、展开“安全性”,“登录名”;“sa”,右键选择“属性”

5、左侧选择“常规”,右侧选择“SQL Server 身份验证”,并设置密码

6、右击数据库选择“方面”

7、在右侧的方面下拉框中选择“服务器配置”;将“RemoteAccessEnabled”属性设为“True”,点“确定”

8、至此SSMS已设置完毕,先退出,再用sa登录,成功即表示sa帐户已经启用

9、打开sql server配置管理器

10、下面开始配置SSCM,选中左侧的“SQL Server服务”,确保右侧的“SQL Server”以及“SQL Server Browser”正在运行

11、在左则选择sql server网络配置节点下的sqlexpress的协议,在右侧的TCP/IP默认是“否”,右键启用或者双击打开设置面板将其修改为“是”

12、选择“IP 地址”选项卡,设置TCP的端口为“1433”

13、将"客户端协议"的"TCP/IP"也修改为“Enabled”

配置完成,重新启动SQL Server 2008。此时应该可以使用了,但是还是要确认一下防火墙。打开防火墙设置。将SQLServr.exe(C:\Program Files\Microsoft SQL Server\MSSQL10.SQLEXPRESS\MSSQL\Binn\sqlservr.exe)添加到允许的列表中。

C语言连接SQLserver问题!

1、确定目标Sqlserver是否允许远程访问

2、确定目标SqlServer服务端口是否是默认端口

3、检查数据库名称、密码什么是否正确

C#中使用SQLServer的方法

1、添加引用

using System.Data.SqlClient;

2、建立连接调用

SqlConnection myConnection = new SqlConnection("数据库连接字符串");

//数据库连接字符串通常是Data Source=localhost;Initial Catalog=数据库名;User ID=用户名;Password=密码

SqlCommand myCommand = new SqlCommand();

myCommand.CommandText = string.Format("select count(*) from {0} where columName={1}",表明,列值);//构造SQL查询语句     String.Format (String, Object[]) 将指定 String 中的格式项替换为指定数组中相应 Object 实例的值的文本等效项。        myCommand.Connection = myConnection;

try

{

myCommand.Connection.Open();

int count = (int)myCommand.ExecuteScalar();

if (count  0)   

{

//count大于0表示有,调用自己写的一个方法来更新

UpdateData();

}

else

{

小于0表示没有,调用这个方法来插入            

InsertData();

}

}

catch (Exception ex)

{

Response.Write(ex.ToString());

}

//UpdateData方法    

public void UpdateData()

{

SqlConnection myConnection = new SqlConnection("数据库连接字符串");

SqlCommand myCommand = new SqlCommand();

myCommand.CommandText = "用来更新的SQL语句";

myCommand.Connection = myConnection;

try

{

myCommand.Connection.Open();

myCommand.ExecuteNonQuery();

}

catch (Exception ex)

{

Response.Write(ex.ToString());

}

}

//InsertData方法 

public void InsertData()

{

SqlConnection myConnection = new SqlConnection("数据库连接字符串");

SqlCommand myCommand = new SqlCommand();

myCommand.CommandText = "用来插入的SQL语句";

myCommand.Connection = myConnection;

try

{

myCommand.Connection.Open();

myCommand.ExecuteNonQuery();

}

catch (Exception ex)

{

Response.Write(ex.ToString());

}

}

-----这些都是基础的写法,可以将其封装在一个工具类中,方便调用。

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

using System.Configuration;

using System.Data;

using System.Data.SqlClient;

namespace DBUtility

{

public class SqlHelper

{

//通过配置文件(app.config:xml)读取连接字符串

public static string connectionString = ConfigurationManager .ConnectionStrings["ConnectionString"].ConnectionString;

//字段,通过连接字符串获取连接对象

private SqlConnection con = new SqlConnection(connectionString);

//属性,判断连接对象的状态并打开连接对象

public SqlConnection Con

{

get {

switch (con.State)

{

case ConnectionState.Broken:

con.Close(); //先正常关闭,释放资源

con.Open();

break;

case ConnectionState.Closed:

con.Open();

break;

case ConnectionState.Connecting:

break;

case ConnectionState.Executing:

break;

case ConnectionState.Fetching:

break;

case ConnectionState.Open:

break;

default:

break;

}

return con; }

set { con = value; }

}

//执行存储过程或者SQL语句并返回数据集DataSet

public DataSet GetDataSet(string strSQL, CommandType cmdType, params SqlParameter[] values)

{

SqlCommand cmd = PrepareCommand(strSQL, cmdType, values);

SqlDataAdapter da = new SqlDataAdapter(cmd);

DataSet ds = new DataSet();

da.Fill(ds);

return ds;

}

//执行存储过程或者SQL语句并返回SqlDatareader

public SqlDataReader GetDataReader(string strSQL, CommandType cmdType, params SqlParameter[] values)

{

SqlCommand cmd = PrepareCommand(strSQL, cmdType, values);

SqlDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);

return dr;

}

//执行存储过程或者SQL语句并返回首行首列(新增方法的主键)

public object ExecuteScalar(string strSQL, CommandType cmdType, params SqlParameter[] values)

{

SqlCommand cmd = PrepareCommand(strSQL, cmdType, values);

return cmd.ExecuteScalar();

}

//执行存储过程或者SQL语句并返回受影响行数

public int ExecuteNonQuery(string strSQL, CommandType cmdType, params SqlParameter[] values)

{

SqlCommand cmd = PrepareCommand(strSQL, cmdType, values);

return cmd.ExecuteNonQuery();

}

//内部方法,实例化命令对象并配置相关属性

private SqlCommand PrepareCommand(string strSQL, CommandType cmdType,params SqlParameter[] values)

{

SqlCommand cmd = new SqlCommand();

cmd.Connection = Con;

cmd.CommandText = strSQL;

cmd.CommandType = cmdType;

cmd.CommandTimeout = 60;

cmd.Parameters.AddRange(values);

return cmd;

}

}

}

c++中怎么连接sqlserver

C++连接SQL数据库第一步 系统配置

1.设置SQLSERVER服务器为SQL登录方式,并且系统安全性中的sa用户要设置登录功能为“启用”,还有必须要有密码。

2.需要在ODBC中进行数据源配置,数据源选\”SQL SERVER”,登录方式使用“使用输入用户登录ID和密码的SQL SERVER验证”,并填写登录名(sa)和密码,注意一点,密码不能为空,这就意味着你的sa用户必须得有密码。否则无法通过系统本身的安全策略。测试通过就完成了配置。

C++连接SQL数据库第二步 C++与SQL连接初始化

1.在你所建立的C++项目中的stdafx.h头文件中引入ADO

具体代码如下

#import “c:\Program Files\Common Files\System\ado\msado15.dll”

no_namespace rename(”EOF”, “adoEOF”) rename(”BOF”, “adoBOF”)

2.定义_ConnectionPtr变量后调用Connection对象的Open方法建立与服务器的连接。

数据类型_ConnectionPtr实际上是由类模板_com_ptr_t得到的一个具体的实例类。_ConnectionPtr类封装了Connection对象的Idispatch接口指针及其一些必要的操作。可以通过这个指针操纵Connection对象。

例如连接SQLServer数据库,代码如下:

//连接到MS SQL Server

//初始化指针

_ConnectionPtr pMyConnect = NULL;

HRESULT hr = pMyConnect.CreateInstance(__uuidof(Connection));

if (FAILED(hr))

return;

//初始化链接参数

_bstr_t strConnect = “Provider=SQLOLEDB;

Server=hch;

Database=mytest;

uid=sa; pwd=sa;”; //Database指你系统中的数据库

//执行连接

try

{

// Open方法连接字串必须四BSTR或者_bstr_t类型

pMyConnect-Open(strConnect, “”, “”, NULL);

}

catch(_com_error e)

{

MessageBox(e.Description(), “警告”, MB_OK|MB_ICONINFORMATION);

}//发生链接错误

C++连接SQL数据库第三步 简单的数据连接

//定义_RecordsetPtr变量,调用它Recordset对象的Open,即可打开一个数据集

//初始化过程 以下是个实例

_RecordsetPtr pRecordset;

if (FAILED(pRecordset.CreateInstance(__uuidof(Recordset))))

{

return;

}

//执行操作

try

{

pRecordset-Open(_variant_t(”userinfo”),

_variant_t((IDispatch*)pMyConnect),

adOpenKeyset, adLockOptimistic, adCmdTable);

}

catch (_com_error e)

{

MessageBox(”无法打开userinfo表\”, “系统提示”,

MB_OK|MB_ICONINFORMATION);

}

C++连接SQL数据库第四步 执行SQL语句

这里是关键,我认为只要你懂点SQL语句那么一切都会方便许多比用上面的方法简单,更有效率点。

首先

m_pConnection.CreateInstance(_uuidof(Connection));

//初始化Connection指针

m_pRecordset.CreateInstance(__uuidof(Recordset));

//初始化Recordset指针

CString strSql=”select * from tb_goods”;//具体执行的SQL语句

m_pRecordset=m_pConnection-Execute(_bstr_t(strSql),

NULL, adCmdText);//将查询数据导入m_pRecordset数据容器

至此 你的SQL语句已经执行完成了m_pRecordset内的数据就是你执行的结果。

取得记录:

while(!m_pRecordset-adoEOF)//遍历并读取name列的记录并输出

{

CString temp = (TCHAR *)(_bstr_t)m_pRecordset-GetFields()-GetItem

(”name”)-Value;

AfxMessageBox(temp);

pRecordset-MoveNext();

}

插入记录

//记得初始化指针再执行以下操作

CString strsql;

strsql.Format(”insert into tb_goods(no,name, price)

values(’%d’,'%s’, %d)”,m_intNo,m_strName,m_intPrice);

m_pRecordset=m_pConnection-

Execute(_bstr_t(strsql),NULL,adCmdText);

修改记录

CString strsql;

strsql.Format(”update tb_goods set name=’%s’ ,

price=%d where no=%d “,m_strName,m_intPrice,m_intNo);

m_pRecordset=m_pConnection-Execute(_bstr_t(strsql),NULL,adCmdText);

删除记录

CString strsql;

strsql.Format(”delete from tb_goodswhere no= ‘%d’ “,m_intNo);

m_pRecordset=m_pConnection-Execute(_bstr_t(strsql),NULL,adCmdText)


网页题目:c访问sqlserver,c访问sqlserver数据库
分享网址:http://ybzwz.com/article/dsspihi.html