把SQL 连接到C#中显示如下错误:,我的代码是这样的:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string str = "server=localhost;database=09130104;intergrated security=true;";
private void Form1_Load(object sender, EventArgs e)
{ } private void button1_Click(object sender, EventArgs e)
{
try
{
SqlConnection conn = new SqlConnection(str);
conn.Open();
SqlCommand comm = new SqlCommand("select * from student where sno=textbox1.text", conn);
DataTable dt = new DataTable();
dt.Load(comm.ExecuteReader());
dataGridView1.DataSource = dt;
}
catch (SqlException sm)
{
MessageBox.Show(sm.Message);
}
finally
{
conn.Close();
}
}
}
}
求解答
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
string str = "server=localhost;database=09130104;intergrated security=true;";
private void Form1_Load(object sender, EventArgs e)
{ } private void button1_Click(object sender, EventArgs e)
{
try
{
SqlConnection conn = new SqlConnection(str);
conn.Open();
SqlCommand comm = new SqlCommand("select * from student where sno=textbox1.text", conn);
DataTable dt = new DataTable();
dt.Load(comm.ExecuteReader());
dataGridView1.DataSource = dt;
}
catch (SqlException sm)
{
MessageBox.Show(sm.Message);
}
finally
{
conn.Close();
}
}
}
}
求解答
解决方案 »
- 史上最简单好用的.net图表控件,仅仅需要两句代码就能搞定一个漂亮的图表,无需对配色有任何了解,真是程序员的福音,不得不花50分推荐一下
- IBatis怎么设置CommandTimeout属性
- 【提问】字符串操作
- 在GridView中动态为PagerTemplate添加LinkButton,但是无法为LinkButton绑定事件
- 郁闷: 我的FreeTextBox为什么不能获取输入焦点??
- DATAGRID中删除列的确认框问题 在线等!
- ASP.NET 2.0如何实现大文件上传
- 再次询问有关时间的问题。
- 如何做这样的事件?
- 请问这个上传程序错在那啊?怎么传大的文件就出错
- 项目问题
- asp.net 使用ckeditor 3.6编辑器,数据在label中显示问题
SqlCommand comm = new SqlCommand("select * from student where sno=textbox1.text", conn);
改成
string sno=textbox1.Text.Trim();
string selectSQL = string.format("select * from student where sno={0}",sno);
SqlCommand comm = new SqlCommand(selectSQL);
试试,当然你要注意sno字段的数据类型,以及空值判断
SqlCommand comm = new SqlCommand("select * from student where sno=textbox1.text", conn);
改成
string sno=textbox1.Text.Trim();
string selectSQL = string.format("select * from student where sno={0}",sno);
SqlCommand comm = new SqlCommand(selectSQL);
试试,当然你要注意sno字段的数据类型,以及空值判断
看不到图估计是链接不上DB出错了微软提供了以下四种数据库连接方式:System.Data.OleDb.OleDbConnection
System.Data.SqlClient.SqlConnection
System.Data.Odbc.OdbcConnection
System.Data.OracleClient.OracleConnection
[/code下面我们以范例的方式,来依次说明:
System.Data.SqlClient.SqlConnection
[/code]
常用的一些连接字符串(C#代码):SqlConnection conn
= new SqlConnection( "Server=(local);Integrated Security=SSPI;database=Pubs");SqlConnection conn
= new SqlConnection("server=(local)//NetSDK;database=pubs;Integrated Security=SSPI");SqlConnection conn = new SqlConnection(
"Data Source=localhost;Integrated Security=SSPI;Initial Catalog=Northwind;");SqlConnection conn = new SqlConnection(
" data source=(local);initial catalog=xr;integrated security=SSPI;
persist security info=False;workstation id=XURUI;packet size=4096; ");SqlConnection myConn = new
System.Data.SqlClient.SqlConnection("Persist Security Info=False;Integrated
Security=SSPI;database=northwind;server=mySQLServer");SqlConnection conn = new SqlConnection(
" uid=sa;pwd=passwords;initial catalog=pubs;data source=127.0.0.1;Connect Timeout=900");