private void button1_Click(object sender, EventArgs e)
{
try
{
string strConnection = "server=.;user id = sa;password = 3661849;database=Student11";
SqlConnection objConnection = new SqlConnection(strConnection);
objConnection.Open();
button1.Text = "OK";
SqlCommand com = new SqlCommand("insert Table_1 values('" + textBox1.Text.ToString() + "','" + textBox2.Text.ToString() + "')", objConnection);
com.CommandText = "select * from Table_1";
com.Connection = objConnection;
SqlDataAdapter data = new SqlDataAdapter();
data.SelectCommand = com;
DataSet ds = new DataSet();
data.Fill(ds, "Table_1");
DataGridView.DataSource = ds.Tables[0];//DataSource这里报错,但不知道错哪了,还有,C#无法连接数据库怎么办,各种办法都试了。
objConnection.Close();
}
catch (SqlException exc)
{
MessageBox.Show(exc.Message);
}
}
{
try
{
string strConnection = "server=.;user id = sa;password = 3661849;database=Student11";
SqlConnection objConnection = new SqlConnection(strConnection);
objConnection.Open();
button1.Text = "OK";
SqlCommand com = new SqlCommand("insert Table_1 values('" + textBox1.Text.ToString() + "','" + textBox2.Text.ToString() + "')", objConnection);
com.CommandText = "select * from Table_1";
com.Connection = objConnection;
SqlDataAdapter data = new SqlDataAdapter();
data.SelectCommand = com;
DataSet ds = new DataSet();
data.Fill(ds, "Table_1");
DataGridView.DataSource = ds.Tables[0];//DataSource这里报错,但不知道错哪了,还有,C#无法连接数据库怎么办,各种办法都试了。
objConnection.Close();
}
catch (SqlException exc)
{
MessageBox.Show(exc.Message);
}
}
解决方案 »
- VS2010编译时报错:'SelectByName' is not supported by language
- 至少一个参数没有被指定值
- 为什么 GetResponse 经常获取不到任何数据?
- 关于datagridview中删除多条数据时的速度问题
- 我使用SharpPcap抓到了一些包。如何重组这些包?
- 在.Net中有没有读写outlook地址簿*.wab文件的类?
- winfrom 中实现导入EXCEL 与 SQL 表建立列对应关系??
- C#自定义属性问题
- 還原編輯前的數據
- 紧急求教:如何用VS.NET将windows程序最终生成SETUP.EXE
- .net(C#) winform开发实现数据库报表以excel导出!!!求高手指点!!!
- 关于TabPage中控件的Visible属性问题
请检查表名是否正确Table_1;
另外,把报错内容附上,别人才知道你哪错了。
不要他 Table_1 试试 他只是个别名 我很少写上
在这一行的左边点个红色的点点 就在代码编辑的那一块 别点在工具栏里面了
string strConnection = "data source=.;initial catalog=Student11;user id = sa;password = 3661849;";
using ( SqlConnection objConnection = new SqlConnection(strConnection) )
{
objConnection.Open();
SqlDataAdapter data = new SqlDataAdapter("select * from Table_1", objConnectio);
DataSet ds = new DataSet();
data.Fill(ds, "Table_1");
DataGridView.DataSource = ds.Tables["Table_1"];
DataGridView.DataBind();
}
试试
* 程序名称:ConnSqlServer.Form1.cs
* 程序功能:连接SQL Server数据库
*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Sql;
using System.Data.SqlClient;namespace ConnSqlServer
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private SqlConnection SqlCon;
//连接状态
private string state = null;
//建立连接并打开
private void btnConn_Click(object sender, EventArgs e)
{
//得到服务器名、用户名、密码、数据库名等参数
string strServer = txtServer.Text.Trim();
string strUser = txtUser.Text.Trim();
string strPwd = txtPwd.Text.Trim();
string strDataBase = txtDataBase.Text.Trim();
//构造连接字符串
string ConStr = string.Format("server={0};user id={1};pwd={2};database={3}", strServer, strUser, strPwd, strDataBase);
//初始化连接
SqlCon = new SqlConnection(ConStr);
//打开连接
SqlCon.Open();
//得到连接状态
state = SqlCon.State.ToString();
if (state == "Open")
{
//提示
MessageBox.Show("连接数据库成功!");
btnClose.Enabled = true;
btnConn.Enabled = false;
} }
//执行Sql语句
private void btnExec_Click(object sender, EventArgs e)
{
//得到连接状态
state = SqlCon.State.ToString();
if (state == "Open")
{
//查询语句
string strsql = txtSql.Text.ToString();
//执行SQL语句
using (SqlDataAdapter sda = new SqlDataAdapter(strsql, SqlCon))
{
try
{
DataSet ds = new DataSet();
//填充DataSet
sda.Fill(ds, "table");
//数据绑定
this.dataGridView1.DataSource = ds.Tables[0].DefaultView;
}
catch
{
return;
}
}
}
else
{
MessageBox.Show("当前连接状态:" + state);
btnConn.Enabled = true;
}
}
//关闭连接
private void btnClose_Click(object sender, EventArgs e)
{
//得到连接状态
state = SqlCon.State.ToString();
if (state == "Open")
{
//关闭连接
SqlCon.Close();
//释放连接占用的资源
SqlCon.Dispose();
MessageBox.Show("连接已成功被关闭");
btnConn.Enabled = true;
}
else
{
MessageBox.Show("当前的连接为关闭状态!"); }
btnClose.Enabled = false;
}
}
}
代码贴给你吧
dataGridView1.DataSource = ds.Tables[0];
这里sql语句错了把 insert into 你的 into 呢
这里改成DataGridView.DataSource = ds.Tables[0].DefaultView; 试试
如果没改应该是 DataGridView1
2 DataGridView是类名 你都没实例化 怎么给DataSource赋值 这个又不是静态属性