这是form1.cs 的代码,这里正常运行
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 TestDb
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
Conn con = new Conn(this.textBox1.Text, this.textBox2.Text, this.textBox3.Text, this.textBox4.Text);
SqlConnection sqlConnection = con.creatConn();
SqlCommand cmd =new SqlCommand("SELECT COUNT(*)FROM Users WHERE UserName='"+this.textBox5.Text+"'and Password ='"+this.textBox6.Text+"'",sqlConnection);
string t = cmd.ExecuteScalar().ToString();
cmd.Connection.Close();
if (t == "1")
{
this.Visible = false;
Login login = new Login();
login.ShowDialog();
}
else
{
MessageBox.Show("登录失败", "提醒");
}
} private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
}
}这是我的conn.cs 在运行到thisConnection.Open();时出错using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;namespace TestDb
{
class Conn
{
private string IP;
private string DbName;
private string DbUser;
private string Key; public Conn(string ip, string dbName, string dbUser, string key)
{
IP = ip;
DbName = dbName;
DbUser = dbUser;
Key = key;
}
public SqlConnection creatConn()
{
SqlConnection thisConnection = new SqlConnection(@"Server="+IP+";user id="+DbUser+";initial catalog="+DbName+";password="+Key);
thisConnection.Open();
return thisConnection;
}
}
}
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 TestDb
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
} private void button1_Click(object sender, EventArgs e)
{
Conn con = new Conn(this.textBox1.Text, this.textBox2.Text, this.textBox3.Text, this.textBox4.Text);
SqlConnection sqlConnection = con.creatConn();
SqlCommand cmd =new SqlCommand("SELECT COUNT(*)FROM Users WHERE UserName='"+this.textBox5.Text+"'and Password ='"+this.textBox6.Text+"'",sqlConnection);
string t = cmd.ExecuteScalar().ToString();
cmd.Connection.Close();
if (t == "1")
{
this.Visible = false;
Login login = new Login();
login.ShowDialog();
}
else
{
MessageBox.Show("登录失败", "提醒");
}
} private void button2_Click(object sender, EventArgs e)
{
this.Close();
}
}
}这是我的conn.cs 在运行到thisConnection.Open();时出错using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;namespace TestDb
{
class Conn
{
private string IP;
private string DbName;
private string DbUser;
private string Key; public Conn(string ip, string dbName, string dbUser, string key)
{
IP = ip;
DbName = dbName;
DbUser = dbUser;
Key = key;
}
public SqlConnection creatConn()
{
SqlConnection thisConnection = new SqlConnection(@"Server="+IP+";user id="+DbUser+";initial catalog="+DbName+";password="+Key);
thisConnection.Open();
return thisConnection;
}
}
}
没有数据库,
initial catalog="+DbName+";去掉试试
或
SqlConnection thisConnection = new SqlConnection(@"Server="+IP+";database=“+mydatabase+";initial catalog=SSPI);
改成下面的试试
"data source="+IP+";database="+DbName+";user id="+DbUser+";password="+Key
{
SqlConnection thisConnection = new SqlConnection(@"Server="+IP+";user id="+DbUser+";initial catalog="+DbName+";password="+Key);
thisConnection.Open();//把这个去掉
return thisConnection;
}
、、、、、、、、、、、、、、、、、、、、、、、、、 private void button1_Click(object sender, EventArgs e)
{
Conn con = new Conn(this.textBox1.Text, this.textBox2.Text, this.textBox3.Text, this.textBox4.Text);
SqlConnection sqlConnection = con.creatConn();
sqlConnection.OPen()//写在这里
SqlCommand cmd =new SqlCommand("SELECT COUNT(*)FROM Users WHERE UserName='"+this.textBox5.Text+"'and Password ='"+this.textBox6.Text+"'",sqlConnection);
string t = cmd.ExecuteScalar().ToString();
cmd.Connection.Close();
if (t == "1")
{
this.Visible = false;
Login login = new Login();
login.ShowDialog();
}
else
{
MessageBox.Show("登录失败", "提醒");
}
}
{
conn.Open();
}
catch(Excetpiont exp)
{
MessageBox.Show(exp.Message);
}
try{}catch{},绝对好东西
sorry , 可以这样写,但是问题一样没解决!
错误提示:在建立与服务器的连接时出错。在连接到 SQL Server 2005 时,在默认的设置下 SQL Server 不允许进行远程连接可能会导致此失败。 (provider: 命名管道提供程序, error: 40 - 无法打开到 SQL Server 的连接)
这些是通用的,都可以设置的
=========================
错误信息:
“在建立与服务器的连接时出错。在连接到 SQL Server 2005 时,在默认的设置下 SQL Server 不允许进行远程连接可能会导致此失败。 (provider: 命名管道提供程序, error: 40 - 无法打开到 SQL Server 的连接)”
解决方案:1.安装一个SQL Server Management Studio Express。微软官方有下载,baidu,google里面都能查到。2.安装完成后,开始菜单里找到Microsoft SQL Server 2005--配置工具--SQL Server 外围应用配置器--服务和连接的外围应用配置器:
打开MSSQLSERVER节点下的Database Engine 节点,选择"远程连接",再选择"同时使用TCP/IP和Named pipes",确定后重启数据库服务。3.用默认Windows身份验证模式登录SQL Server Management Studio Express,选择左边列表里面的服务器,右键查看属性,在"安全性"里面选择SQL Server和 Windows身份验证模式4.回到SQL Server Management Studio Express主界面,在"安全性"-"登录名"下面为自己新建一个登录名,设置为SQL Server身份验证,然后在左边选择自己 想要的权限。5.完上面四步后,通过以下连接字符串就应该可以连接到SQL Server 2005了。"data source=.\SQLEXPRESS;initial catalog=master;uid=sa;pwd=sa"其中.\SQLEXPRESS可以写作LOCALHOST\SQLEXPRESS或者MachineName\LOCALHOST,当然第一种写法最简单了,适用于本机。
我用的是sql205 开发版 ,安装SQL Server Management Studio Express时出现 以下提示 ,不能安装。
“安装程序检测到本机上装有 SQL Server Management Studio 的某个版本。SQLServer Management Studio 和 SQL Server Management Studio Express不能安装在同一计算机上。 若要安装 SQLServer Management Studio Express,必须先卸载SQL Server Management Studio,...” 请问我可以卸载SQL Server Management Studio 吗?
1。分层
2。命名规范
3。防SQL注入
4。try catch
养成好的编程习惯
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient; namespace TestDb
{
class Conn
{
private string IP;
private string DbName;
private string DbUser;
private string Key;
public qlConnection QL=new sqlConnection() ; public Conn(string ip, string dbName, string dbUser, string key)
{
IP = ip;
DbName = dbName;
DbUser = dbUser;
Key = key;
}
public void creatConn()
{
QL.ConnectionString(@"Server="+IP+";user id="+DbUser+";initial catalog="+DbName+";password="+Key);//ConnectionString这个单词记不清了,可能是这么拼写
QL.open();
MessageBox.show("链接成功!")
}
public ~Conn(){QL.close("关闭成功!");}
}
}
然后在调用:
Conn con = new Conn(this.textBox1.Text, this.textBox2.Text, this.textBox3.Text, this.textBox4.Text);
con.creatConn();
SqlCommand cmd =new SqlCommand("SELECT COUNT(*)FROM Users WHERE UserName='"+this.textBox5.Text+"'and Password ='"+this.textBox6.Text+"'",con.QL);
string t = cmd.ExecuteScalar().ToString();
con.~Conn();
}
应该可实现了
SqlConnection thisConnection = new SqlConnection("server=.;trusted_connection=yes;user=sa;pwd=1;database="); 其中user和pwd是根据你装的数据库选项确定的,database是数据库名
招聘职位:VC高级程序员招聘:C++程序员
工作地点:北京市东三环国贸附近
薪水范围:待遇优厚
学历要求:招聘没有学历限制,只看重素质修养和学习能力。
1.熟悉VC++编程,熟悉MFC、Windows API
2.熟悉socket , 或多线程编程, 或 有window用户界面开发经验
3.具有精益求精的职业素质和快速学习的适应能,善于发现并解决问题;
4.独立设计过软件产品或者产品模块者优先考虑***满足上面3-4点要求即***
有意者请将简历、联系方法、待遇要求通过E-mail,以邮件主题“xxx应聘xx职位”发至[email protected]
<connectionStrings>
<add name="Personal" connectionString="Data Source=.\SQLExpress;Integrated Security=True;User Instance=True;AttachDBFilename=|DataDirectory|Personal.mdf" providerName="System.Data.SqlClient"/>
<remove name="LocalSqlServer"/>
<add name="LocalSqlServer" connectionString="Data Source=.\SQLExpress;Integrated Security=True;User Instance=True;AttachDBFilename=|DataDirectory|aspnetdb.mdf"/>
</connectionStrings>