c# SQL 2005 连接问题 C# 学习 2天。 窗体控件 简单的会用了。 但是我想用 C# 连接 SQL2005 数据库 不知道 怎么写代码。 以及 做一个 数据库连接的按钮。 BUTTON 点击按钮 进行数据库连接。如果连接成功 提示 OK 。 MESSAGE 框 。 不能成功 提示NO MESSAGE 框包括表头的代码 能否给我看下 参考 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 在C#中连接C# 连接 SQL2005的代码是方法一:只不过要在程序的前面添加引用:using system.data.sqlclient;Sqlconnection mycon=new sqlconnection("server=(local);data=数据库存名称;user=用户名;pwd=密码");方法二:在web.config文件中配置好:<connectionStrings> <clear/> <add name="SQLSessionConn" connectionString="Server=127.0.0.1;Persist Security Info=True;Initial Catalog=数据库名称;User ID=用户名;Password= 密码" providerName="System.Data.SqlClient"/> <add name="BoBoConn" connectionString="Server=127.0.0.1;Persist Security Info=True;Initial Catalog=数据库名称;User ID=用户名;Password=密码" providerName="System.Data.SqlClient"/> </connectionStrings>再在后台的代码中去写:先添加引用:using system.data.sqlclient;写 SqlConnection mycon = new SqlConnection(); mycon.ConnectionString = ConfigurationManager.ConnectionStrings["BoBoConn"].ToString(); 在App.config的<connectionStrings>add一个数据库连接,如下<add name="ConnectionString" connectionString="Data Source=192.168.1.1;Initial Catalog=master;Persist Security Info=True;User ID=dbo;Password=dbo" providerName="System.Data.SqlClient" /> using System;using System.Collections.Generic;using System.Text;using System.Data.SqlClient;using System.Data;namespace ConsoleApplication1{ class Program { static void Main(string[] args) { // "uid=sa":连接数据库的用户名为sa. // "password=":连接数据库的验证密码为空.他的别名为"pwd",所以我们可以写为"pwd=". // "initial catalog=Northwind":使用的数据源为"Northwind"这个数据库.他的别名为"Database",本句可以写成"Database=Northwind". // "Server=YourSQLServer":使用名为"YourSQLServer"的服务器.他的别名为"Data Source","Address","Addr". // " Connect Timeout=30":连接超时时间为30秒.(根据情况添加) // PS: // 1.你的SQL Server必须已经设置了需要用户名和密码来登录,否则不能用这样的方式来登录.如果你的SQL Server设置为Windows登录,那么在这里就不需要使用"uid"和"password"这样的方式来登录,而需要使用"Trusted_Connection=SSPI"来进行登录. // 2. 如果使用的是本地数据库且定义了实例名,则可以写为"Server=(local)\实例名";如果是远程服务器,则将"(local)"替换为远程服务器的名称或IP地址. string strConnection = "Trusted_Connection=SSPI;"; strConnection += "database=NTF_Navision_enlistment60;Server=CAIXIATA-6BE823;"; strConnection += "Connect Timeout=30"; using (SqlConnection objConnection = new SqlConnection(strConnection)) { objConnection.Open(); // method 1 SqlCommand myCommand = new SqlCommand("select * from Couse", objConnection); Console.WriteLine("open"); SqlDataReader myReader = myCommand.ExecuteReader(); while (myReader.Read()) { Console.WriteLine(myReader.GetFieldType(0)); Console.WriteLine(myReader.GetFieldType(1)); Console.WriteLine(myReader.GetFieldType(2)); Console.WriteLine(myReader.GetDecimal(0)); Console.WriteLine(myReader.GetDecimal(1)); Console.WriteLine(myReader.GetDecimal(2)); } myReader.Close(); // method 2 SqlDataAdapter myCommandd = new SqlDataAdapter("select * from Couse", objConnection); DataSet ds = new DataSet(); myCommandd.Fill(ds, "Couse"); DataTable dt = ds.Tables["couse"]; Console.WriteLine(dt.Columns[0].ToString()); Console.WriteLine(dt.DefaultView.Count); } } }} http://blog.csdn.net/LCL_data/archive/2009/04/30/4139145.aspx 不要 WEB 的 要。 C/S模式下的哦。 objConnection.Open();打开了。要记得关闭一下了啊。 但是 做一个 数据库连接的按钮。 BUTTON 点击按钮 进行数据库连接。如果连接成功 提示 OK 。 MESSAGE 框 。 不能成功 提示NO MESSAGE 框 这个部分怎么做? 我才学两天啊。 能否把代码 COPY 出来啊。 不要 复制别人的东西。 我该看的都看了 using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Text;using System.Windows.Forms;using System.Data.SqlClient;namespace WindowsApplication1{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } /// <summary> /// 创建时间:2009-6-12 /// 创建人:周昕 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void button1_Click(object sender, EventArgs e) { string sqlconnstr = "server=(local);database=BoBo;user id=sa;pwd=123456"; SqlConnection mycon = new SqlConnection(sqlconnstr); string state = mycon.State.ToString(); if (state == "Closed") { //连接关闭的时刻去打开边接 mycon.Open(); MessageBox.Show("连接已经关闭"); } else { //连接打开的时刻去关闭连接 mycon.Close(); MessageBox.Show("连接已经打开"); } } } } LINQ 是什么?请仔细 谢谢 public static string con="server=.;database= 数据库名;uid=sa;pwd=accp";sa 是登陆数据库的登录名;pwd 是:登陆数据库的密码;string sql="select * from table";sqlconnection sql=new sqlconnection(sql,con);这就是数据库的链接,剩余的只是判读是否链接上,判断是否链接,有许多种方法。可以是否查处数据为根据进行判断 求正则表达式问题 C#怎么让webBrowser中的新弹出的页面显示在另一个webBrowser 安装包重写Install 问一下,winform大家用何种方式显示数据 多线程程序退出问题 未指定的错误: E_FAIL(0x80004005),谁知道解决办法? 再次求助关于调用含指针的C++DLL的问题 请教:用ado.net实现listview 数据填充问题 WPF,请问这样的UserControl怎么创建? 各位版主与各位.Net区的兄弟们注意。 又来送分啦!!哈哈,快来呀! C#获取EXCEL的操作
方法一:
只不过要在程序的前面添加引用:using system.data.sqlclient;
Sqlconnection mycon=new sqlconnection("server=(local);data=数据库存名称;user=用户名;pwd=密码");
方法二:
在web.config文件中配置好:
<connectionStrings>
<clear/>
<add name="SQLSessionConn" connectionString="Server=127.0.0.1;Persist Security Info=True;Initial Catalog=数据库名称;User ID=用户名;Password= 密码" providerName="System.Data.SqlClient"/>
<add name="BoBoConn" connectionString="Server=127.0.0.1;Persist Security Info=True;Initial Catalog=数据库名称;User ID=用户名;Password=密码" providerName="System.Data.SqlClient"/>
</connectionStrings>
再在后台的代码中去写:
先添加引用:using system.data.sqlclient;
写 SqlConnection mycon = new SqlConnection();
mycon.ConnectionString = ConfigurationManager.ConnectionStrings["BoBoConn"].ToString();
<add name="ConnectionString" connectionString="Data Source=192.168.1.1;Initial Catalog=master;Persist Security Info=True;User ID=dbo;Password=dbo"
providerName="System.Data.SqlClient" />
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
// "uid=sa":连接数据库的用户名为sa.
// "password=":连接数据库的验证密码为空.他的别名为"pwd",所以我们可以写为"pwd=".
// "initial catalog=Northwind":使用的数据源为"Northwind"这个数据库.他的别名为"Database",本句可以写成"Database=Northwind".
// "Server=YourSQLServer":使用名为"YourSQLServer"的服务器.他的别名为"Data Source","Address","Addr".
// " Connect Timeout=30":连接超时时间为30秒.(根据情况添加)
// PS:
// 1.你的SQL Server必须已经设置了需要用户名和密码来登录,否则不能用这样的方式来登录.如果你的SQL Server设置为Windows登录,那么在这里就不需要使用"uid"和"password"这样的方式来登录,而需要使用"Trusted_Connection=SSPI"来进行登录.
// 2. 如果使用的是本地数据库且定义了实例名,则可以写为"Server=(local)\实例名";如果是远程服务器,则将"(local)"替换为远程服务器的名称或IP地址.
string strConnection = "Trusted_Connection=SSPI;";
strConnection += "database=NTF_Navision_enlistment60;Server=CAIXIATA-6BE823;";
strConnection += "Connect Timeout=30";
using (SqlConnection objConnection = new SqlConnection(strConnection))
{
objConnection.Open();
// method 1
SqlCommand myCommand = new SqlCommand("select * from Couse", objConnection); Console.WriteLine("open");
SqlDataReader myReader = myCommand.ExecuteReader();
while (myReader.Read())
{
Console.WriteLine(myReader.GetFieldType(0));
Console.WriteLine(myReader.GetFieldType(1));
Console.WriteLine(myReader.GetFieldType(2));
Console.WriteLine(myReader.GetDecimal(0));
Console.WriteLine(myReader.GetDecimal(1));
Console.WriteLine(myReader.GetDecimal(2));
}
myReader.Close(); // method 2
SqlDataAdapter myCommandd = new SqlDataAdapter("select * from Couse", objConnection);
DataSet ds = new DataSet();
myCommandd.Fill(ds, "Couse");
DataTable dt = ds.Tables["couse"];
Console.WriteLine(dt.Columns[0].ToString());
Console.WriteLine(dt.DefaultView.Count);
}
}
}
}
不能成功 提示NO MESSAGE 框
这个部分怎么做? 我才学两天啊。 能否把代码 COPY 出来啊。 不要 复制别人的东西。 我该看的都看了
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
/// <summary>
/// 创建时间:2009-6-12
/// 创建人:周昕
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void button1_Click(object sender, EventArgs e)
{
string sqlconnstr = "server=(local);database=BoBo;user id=sa;pwd=123456";
SqlConnection mycon = new SqlConnection(sqlconnstr);
string state = mycon.State.ToString(); if (state == "Closed")
{
//连接关闭的时刻去打开边接
mycon.Open();
MessageBox.Show("连接已经关闭");
}
else
{
//连接打开的时刻去关闭连接
mycon.Close();
MessageBox.Show("连接已经打开");
}
}
}
}
sa 是登陆数据库的登录名;
pwd 是:登陆数据库的密码;string sql="select * from table";
sqlconnection sql=new sqlconnection(sql,con);这就是数据库的链接,剩余的只是判读是否链接上,判断是否链接,有许多种方法。可以是否查处数据为根据进行判断