怎样打开Sql sqlserver2008r2 实例 2008的SQLEXPRESS实例

52738人阅读
数据库(2)
VS2010连接SQL Server 2008并执行查询操作
先在SQL Server 2008中建一个Student数据库,含有一个表student,4个字段,分别为姓名(varchar)学号(varchar)性别(varchar)年龄(int),并指定一个用户登录该数据库,用户名为cam,密码为123456,注意要修改cam用户的权限
新建控制台应用程序,连接数据库,输出student表中的所有字段,并执行插入删除操作
using System.D
using System.Data.SqlC
using System.Collections.G
using System.L
using System.T
namespace 连接数据库
class Program
public static int Insert(string name, string pwd,string sex,int age)
SqlConnection conn = new SqlConnection(&Data Source=(local);Initial Catalog=SIntegrated Security=True&);//Initial Catalog后面跟你数据库的名字
conn.Open();
string sql = &insert into student(姓名,学号,性别,年龄) values(@name,@pwd,@sex,@age)&;
SqlCommand cmd = new SqlCommand(sql, conn);
SqlParameter parn1 = new SqlParameter(&@name&, name);
cmd.Parameters.Add(parn1);
SqlParameter parn2 = new SqlParameter(&@pwd&, pwd);
cmd.Parameters.Add(parn2);
SqlParameter parn3 = new SqlParameter(&@sex&, sex);
cmd.Parameters.Add(parn3);
SqlParameter parn4 = new SqlParameter(&@age&, age);
cmd.Parameters.Add(parn4);
int result = cmd.ExecuteNonQuery();//result接收受影响行数,也就是说result大于0的话表示添加成功
conn.Close();
cmd.Dispose();
public static int Update(string name)
SqlConnection conn = new SqlConnection(&Data Source=(local);Initial Catalog=SIntegrated Security=True&);//Initial Catalog后面跟你数据库的名字
conn.Open();
string sql = &delete from student where 姓名=@name&;
SqlCommand cmd = new SqlCommand(sql, conn);
SqlParameter parn = new SqlParameter(&@name&,name);
cmd.Parameters.Add(parn);
int result = cmd.ExecuteNonQuery();//result接收受影响行数,也就是说result大于0的话表示删除成功
conn.Close();
cmd.Dispose();
static void Main(string[] args)
//指定Sql Server提供者的连接字符串
string connString = &server=CAMBRIDGE-PC;database =Suid=pwd=123456&;
//建立连接对象
SqlConnection Sqlconn = new SqlConnection(connString);
//打开连接
Sqlconn.Open();
//为上面的连接指定Command对象
SqlCommand thiscommand = Sqlconn.CreateCommand();
mandText = &select 姓名,学号,性别,年龄 from student&;
//为指定的command对象执行DataReader
SqlDataReader thisSqlDataReader = thiscommand.ExecuteReader();
while (thisSqlDataReader.Read())
Console.WriteLine(&{0} {1} {2} {3}&, thisSqlDataReader[&姓名&], thisSqlDataReader[&学号&], thisSqlDataReader[&性别&], thisSqlDataReader[&年龄&]);
//关闭读取
thisSqlDataReader.Close();
int result = Insert(&关羽&, &E&, &男&, 25);
Console.WriteLine(&影响的行数为:{0}&, result);
result = Update(&关羽&);
Console.WriteLine(&影响的行数为:{0}&, result);
//关闭连接
Sqlconn.Close();
Console.ReadLine();
建Windows窗体应用程序也可以,在Form窗体中拖一个DataGridView控件,插入一个学生的信息,在DataGridView控件中显示所有学生的信息
using System.Collections.G
using System.D
using System.Data.SqlC
using System.D
using System.L
using System.T
using System.Windows.F
namespace cam
public partial class Form1 : Form
public Form1()
InitializeComponent();
public static int Insert(string name, string pwd, string sex, int age)
SqlConnection conn = new SqlConnection(&Data Source=(local);Initial Catalog=SIntegrated Security=True&);//Initial Catalog后面跟你数据库的名字
conn.Open();
string sql = &insert into student(姓名,学号,性别,年龄) values(@name,@pwd,@sex,@age)&;
SqlCommand cmd = new SqlCommand(sql, conn);
SqlParameter parn1 = new SqlParameter(&@name&, name);
cmd.Parameters.Add(parn1);
SqlParameter parn2 = new SqlParameter(&@pwd&, pwd);
cmd.Parameters.Add(parn2);
SqlParameter parn3 = new SqlParameter(&@sex&, sex);
cmd.Parameters.Add(parn3);
SqlParameter parn4 = new SqlParameter(&@age&, age);
cmd.Parameters.Add(parn4);
int result = cmd.ExecuteNonQuery();//result接收受影响行数,也就是说result大于0的话表示添加成功
conn.Close();
cmd.Dispose();
public static int Update(string name)
SqlConnection conn = new SqlConnection(&Data Source=(local);Initial Catalog=SIntegrated Security=True&);//Initial Catalog后面跟你数据库的名字
conn.Open();
string sql = &delete from student where 姓名=@name&;
SqlCommand cmd = new SqlCommand(sql, conn);
SqlParameter parn = new SqlParameter(&@name&, name);
cmd.Parameters.Add(parn);
int result = cmd.ExecuteNonQuery();//result接收受影响行数,也就是说result大于0的话表示删除成功
conn.Close();
cmd.Dispose();
public DataTable sel()
SqlConnection conn = new SqlConnection(&Data Source=(local);Initial Catalog=SIntegrated Security=True&);//Initial Catalog后面跟你数据库的名字,如果你的SqlServer服务器名称后面不带SQLEXPRESS,那么Data Source=.
conn.Open();
string sql = &select * from student&;
SqlCommand cmd = new SqlCommand(sql, conn);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
conn.Close();
cmd.Dispose();
private void Form1_Load(object sender, EventArgs e)
Insert(&关羽&, &E&, &男&, 25);
dataGridView1.DataSource = sel();
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:269814次
积分:3873
积分:3873
排名:第6249名
原创:126篇
评论:63条
(1)(11)(52)(52)(11)SQL Server2008数据库表编辑所有行的方法
SQL Server2008数据库在修改表的数据时,默认只能&编辑前200行&和&返回前1000行&,当数据比较多时有些不方便,那有没有办法像SQL Server2005一样可以编辑所有行呢,当然,只需要设置一下即可。
1.打开Microsoft SQL Server Management Studio
2.点击工具菜单,选择&选项&,打开选项设置窗口
3.依次展开选项窗口中左边窗口的SQL Server对象资源管理器&命令,在右边的窗口中将&编辑前&n&行命令的值&和&选择前&n&行命令的值&都改为0即可,相关截图如下所示:
标签(Tag):
------分隔线----------------------------
------分隔线----------------------------SQL SERVER 2008安装的时候提示“该实例名称已在使用”解决办法_图文_百度文库
两大类热门资源免费畅读
续费一年阅读会员,立省24元!
SQL SERVER 2008安装的时候提示“该实例名称已在使用”解决办法
上传于||暂无简介
阅读已结束,如果下载本文需要使用1下载券
想免费下载本文?
定制HR最喜欢的简历
下载文档到电脑,查找使用更方便
还剩11页未读,继续阅读
定制HR最喜欢的简历
你可能喜欢Posts - 43,
Articles - 0,
Comments - 192
Coding是关乎优雅的一件事,吼吼,我滴生活是如此滴灿烂!我滴人生是如此滴辉煌!
13:11 by 立雪三尺, ... 阅读,
查看實例名時可用 1、服务&SQL Server(实例名),默认实例为(MSSQLSERVER)
或在连接企业管理时-查看本地实例
2、通過注冊表 HKEY_LOCAL_MACHINE/SOFTWARE/Microsoft/Microsoft SQL Server/InstalledInstance
sqlcmd/osql sqlcmd -L sqlcmd -Lc osql -L
获取可用實例,以下舉一個例子,根據自己情況改
DECLARE @Table TABLE ( instanceName& sysname NULL)
insert @Table EXEC sys.xp_cmdshell 'sqlcmd -Lc'
--LEFT(@@serverName,CHARINDEX('/',@@serverName+'/')-1) 替代為本機名就行了 , 根據實例命名規則判斷
SELECT * FROM @Table WHERE instanceName LIKE&& LEFT( @@serverName , CHARINDEX ( '/' , @@serverName + '/' )- 1)+ '%'
--1. SELECT SERVERPROPERTY('InstanceName') --2 sp_helpserver --3 select @@SERVERNAME--4 SELECT * FROM SYS.SYSSERVERS--5 SELECT * FROM SYS.SERVERS 三、
EXECUTE xp_regread @rootkey='HKEY_LOCAL_MACHINE', @key='SOFTWARE/Microsoft/Microsoft SQL Server/Instance Names/SQl', @value_name='MSSQLSERVER'
Select Case When SERVERPROPERTY ('InstanceName') Is Null Then @@SERVERNAME Else SERVERPROPERTY ('InstanceName') End
五、在本地或网络得到所有实例名
1、You can do with registry reading , like my code
using S using Microsoft.Win32;namespace SMOTest { &&& class Program &&& { &&&&& static void Main() &&&&& { &&&&&&& RegistryKey rk = Registry.LocalMachine.OpenSubKey(@"SOFTWARE/Microsoft/Microsoft SQL Server"); &&&&&&& String[] instances = (String[])rk.GetValue("InstalledInstances"); &&&&&&& if (instances.Length & 0) &&&&&&& { &&&&&&&&&& foreach (String element in instances) &&&&&&&&&& { &&&&&&&&&&&&& if (element == "MSSQLSERVER") &&&&&&&&&&&&&&&& Console.WriteLine(System.Environment.MachineName); &&&&&&&&&&&&& else &&&&&&&&&&&&&&&& Console.WriteLine(System.Environment.MachineName + @"/" + element); &&&&&&&&&& } &&&&&&& } &&&&& } &&& } }
2、You can use SQLDMO.dll to retrieve the list of SQL Server instances.& The SQLDMO.dll can be found from the "C:/Program Files/Microsoft SQL Server/80/Tools/Bin" folder. Refer this assembly in your project and the following snippet would return a List Object containing the sql server instances.
public static List GetSQLServerInstances()
NameList sqlNameList =
Application app =
var sqlServers = new List();
app = new ApplicationClass();
sqlNameList = app.ListAvailableSQLServers();
foreach (string sqlServer in sqlNameList)
sqlServers.Add(sqlServer);
catch(Exception ex)
//play with the exception.
if (sqlNameList != null)
sqlNameList =
if (app != null)
return sqlS

我要回帖

更多关于 sqlserver2008r2 实例 的文章

 

随机推荐