以连接sql server数据库为例,保证你能做到
1首先添加引用。在项目中右键添加引用,然后找到net,找到Systemdata。然后在后台引用的地方输入using SystemDataSqlClient;
2我只讲后台,前台你看着做啊。看代码。我会以最简单最易理解的方式给你讲解。懂了后你可以自己编写
Private void ToData()
{
SqlConnection conn = new SqlConnection();//创建连接对象
connConnectionString = "data source=(要连接的数据库的服务器的IP,如果是本机的话可以用);initial catalog=(数据库名称);user id=(用户名);pwd=(密码)";//连接字符串
connOpen();//打开数据库
SqlCommand cmd = new SqlCommand();//创建命令对象
cmdConnection = conn;
string sql ="select from 表 where 条件";//创建sql语句
cmdCommandText = sql;
SqlDataReader sd = cmdExecuteReader();//执行命令
if (sdRead())
{
MessageBoxShow("有次用户")
}
}
先从数据库出类名,类名为 :命名空间类名
DbHelper db = new DbHelper();
string className = dbGetFullClassName("题号");
Type t = TypeGetType(className);
Form frm = (Form)ActivatorCreateInstance(t);
frmShowDialog();
如果这样的话,数据库存的应该是题目:
ID,Content
题号,题目
定义个函数
public string GetContent(int 题号){
return 题目;
}
点击题号:GetContent(题号);
上一题:GetContent(题号-1);
下一题:GetContent(题号+1);
在定义一个静态的DBHelper类用于数据库连接,
时直接类名点对象名就可以了。
public static string connString="Data Sourse=;Initial CataLog=数据库名;User Id=数据库登陆名; Pwd=数据库登陆密码";
public static SqlConnection=new SqlConnection(connString);
string tablename = "";
tablename = textBox1TextToString();
//连接字符串(你自己要改的)
const string sqlstring = @"Data Source=\SQLEXPRESS;Initial Catalog=text;Integrated Security=True";
SqlConnection conn = new SqlConnection(sqlstring);
connOpen();
//查询语句
string sql = "select from ";
//表名
sql += tablename; SqlCommand cmd = new SqlCommand(sql, conn); // 创建一个适配器
SqlDataAdapter adapter = new SqlDataAdapter(sql, conn); // 创建DataSet,用于存储数据
DataSet testDataSet = new DataSet(); // 执行查询,并将数据导入DataSet
//adapterFill(testDataSet, "result_data");
adapterFill(testDataSet, tablename);
//自动添加列
dataGridView1AutoGenerateColumns = true;
//数据绑定
dataGridView1DataSource = testDataSetTables[0]; cmdDispose();
connClose();
首先C#连接Sql的方式有很多,我说一下我经常使用比较好理解的方法:
C#连接Sql的步骤:
①:添加引用using SystemDataSqlClient;
②:创建连接字符串;
③:创建SQL执行语句;
③:创建SqlConnection对象;
④:打开连接;
⑤:创建SqlCommand对象;
⑥:关闭连接;
-----------------------------------下面是增加记录的代码----------------------------------
string ConnString = "Data Source=SAWYER-PC;Initial Catalog=InfoDemo;Persist Security Info=True;User ID=sa;Password=123"; //创建连接字符串;
string SQL = "insert into Employee values('" + textBox1Text + "','" + textBox2Text + "','" + comboBox_SexSelectedItemToString() + "','" + dateTimePicker1ValueDateToString() + "','" + dateTimePicker2ValueDateToString() + "','" + comboBox_EdBSelectedItemToString() + "','" + comboBox1SelectedValueToString() + "','" + comboBox7SelectedValueToString() + "','" + comboBox4SelectedValueToString() + "')";//创建SQL执行语句,根据你程序的实际情况;
SqlConnection Conn = new SqlConnection(ConnString);//创建SqlConnection对象;
ConnOpen();//打开连接;
SqlCommand cmd = new SqlCommand(SQL, Conn);创建SqlCommand对象;
cmdExecuteNonQuery();
ConnClose();//关闭连接
----------------------查询,并将查询的结果绑定到 dataGridView1经行显示-------------------------
string ConnString = "Data Source=SAWYER-PC;Initial Catalog=InfoDemo;Persist Security Info=True;User ID=sa;Password=123";
SqlConnection Connection;
SqlDataAdapter Adapter;
string SQL = "select from Employee";
Connection = new SqlConnection(ConnString);
ConnectionOpen();
Adapter = new SqlDataAdapter(SQL, Connection);
DataSet data = new DataSet();
AdapterFill(data);
if (dataTablesCount > 0)
{
dataGridView1DataSource = dataTables[0];
}
ConnectionClose();
以上就是关于谁能给个C# winform连接数据库的例子,并且 *** 作查询返回一些数据库,可以显示出来全部的内容,包括:谁能给个C# winform连接数据库的例子,并且 *** 作查询返回一些数据库,可以显示出来、C# Winform中如何用数据库控制页面的显示、c# winform与数据库的连接等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)