189 8069 5689

c获取sqlserver,c获取cpu使用率

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数据库中表脚本。

using System.Data.SqlClient;

using System.Data;

SqlConnection SqlCon=new SqlConnection("server =服务器;database =数据库;uid =帐号;pwd =密码");

public static DataTable GetTable(string select)

{

try

{

SqlDataAdapter da = new SqlDataAdapter(select, SqlCon);

DataTable dt = new DataTable();

da.Fill(dt);

return dt;

}

catch (Exception ex)

{

throw ex;

}

}

其中select 一般形式是 "select * from table"

C#如何获取SQLSERVER的字段的长度?

楼主你好:

你可以使用下面这个语句查询出你想要表的字段类型、长度等信息。

Column_name是你的表字段信息,

Type_name是你该字段的数据类型,

Type_length是该数据类型的默认长度,

Column_lengh是该数据类型的实际长度,这个字段的值就是你想要的数据

SELECT dbo.sysobjects.name as Table_name,

dbo.syscolumns.name as Column_name,

dbo.systypes.name as Type_name,

dbo.systypes.length as Type_length,

columnproperty(dbo.syscolumns.id,dbo.syscolumns.name,'precision') as Column_lengh

from dbo.syscolumns inner join dbo.sysobjects

on dbo.syscolumns.id = dbo.sysobjects.id

left join dbo.systypes

on dbo.syscolumns.xtype = dbo.systypes.xusertypewhere dbo.sysobjects.name = '你的表名'

and AND dbo.syscolumns.name='你想查询的字段名称'

你在C#中执行这个语句,传入你想要的条件,得到的结果就是你想要的东西,

或者使用SELECT name,length FROM SYSCOLUMNS WHERE ID=OBJECT_ID('表名' ) where name='字段名'

希望对你有帮主,望采纳:)谢谢

C#获取 sqlserver 存储过程返回多表数据

返回的时候用dataset就可以,比如你存储过程这样写

select * from A

select * from B

select * from C

select * from D

select * from D

在执行完存储过程后返回一个dataset

dataset ds = 执行存储过程

ds.tables[0]就是表A

ds.tables[1]就是表B

ds.tables[2]就是表C

剩下的依次类推.

c连接sqlserver 2000,我只需要连接到数据库,能取到数据就行,不需要对数据库的内容进行更改。

using (SqlConnection cn = new SqlConnection("Data Source=(local);Initial Catalog=xxx;User ID=sa;Password=sa"))

{

try

{

cn.Open();

SqlCommand cmd = new SqlCommand();

cmd.Connection = cn;

cmd.CommandText = "select * from tablename";

SqlDataReader dr = cmd.ExecuteReader();

while (dr.Read())

{

...

}

dr.Close();

dr=null;

}

catch

{

MessageBox.Show("连接数据库失败!");

}

}

C语言连接SQLserver问题!

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

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

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


网页名称:c获取sqlserver,c获取cpu使用率
文章URL:http://cdxtjz.cn/article/dsejoed.html

其他资讯