我是C#初學者,想找一個C#+SQL數據庫的實例小程序學習,哪位高手如有,呵呵,希望貢獻一個,Tks!
解决方案 »
- DGV 导出Excel
- 谁做过 耀华 XK3190 A9 的串口开发?(读取称重数据)
- 怎么把图片绑定到 DevExpress.XtraGrid.GridControl 控件中
- 两个form之间调用问题?
- regsvr32(dll)
- 求助HttpWebRequest如何发送带GET的POST
- 如何分离一个cs文件从一个网站中
- datagrid的删除怎么做?windows form的
- 在windowform 中如何打开一个excel文件然后把数据导入到Access数据库中
- 提个很菜的问题,C#和vc.net,vc.net和vc++有什么关系?
- 针对于多用户(24W)人同时在线的处理?
- MDI子窗体的标题栏合并到父窗体的菜单栏
程序代码:
--------------------------------------------------------------------------------using System.Data;
using System.Data.SqlClient;...string strConnection="user id=sa;password=;";
strConnection+="initial catalog=Northwind;Server=YourSQLServer;";
strConnection+="Connect Timeout=30";SqlConnection objConnection=new SqlConnection(strConnection);...objConnection.Open();
objConnection.Close();...
解释:连接SQL Server数据库的机制与连接Access的机制没有什么太大的区别,只是改变了Connection对象和连接字符串中的不同参数.首先,连接SQL Server使用的命名空间不是"System.Data.OleDb",而是"System.Data.SqlClient".其次就是他的连接字符串了,我们一个一个参数来介绍(注意:参数间用分号分隔):
"user id=sa":连接数据库的验证用户名为sa.他还有一个别名"uid",所以这句我们还可以写成"uid=sa".
"password=":连接数据库的验证密码为空.他的别名为"pwd",所以我们可以写为"pwd=".
这里注意,你的SQL Server必须已经设置了需要用户名和密码来登录,否则不能用这样的方式来登录.如果你的SQL Server设置为Windows登录,那么在这里就不需要使用"user id"和"password"这样的方式来登录,而需要使用"Trusted_Connection=SSPI"来进行登录.
"initial catalog=Northwind":使用的数据源为"Northwind"这个数据库.他的别名为"Database",本句可以写成"Database=Northwind".
"Server=YourSQLServer":使用名为"YourSQLServer"的服务器.他的别名为"Data Source","Address","Addr".如果使用的是本地数据库且定义了实例名,则可以写为"Server=(local)\实例名";如果是远程服务器,则将"(local)"替换为远程服务器的名称或IP地址.
"Connect Timeout=30":连接超时时间为30秒. 在这里,建立连接对象用的构造函数为:SqlConnection.
C#调用数据库的增删查改
2008-06-24 01:40 A.M.
using System.Data.SqlClient;//引用命名空间 在方法中写 SqlConnection conn=new SqlConnection("Persist Security Info=False;Data Source=127.0.0.1;Initial Catalog=databasename;User ID=userid;Password=pwd");//连数据库 conn.Open();//打开链接
string sql1= "insert into tablename values('"+textBox1.Text+"'); //增string sql2 ="delete from table1 where qqq='"+textBox1.Text+"'"; //删string sql3 ="select * from tablename where qqq='"+textBox1.Text+"'";//查string sql4 ="update tablename set qqq='"+textBox1.Text+"' where www='"+textBox2.Text+"'';//改SqlCommand cmd=new SqlCommand(sql1,conn);//要执行的语句
int x=cmd.ExecuteNonQuery();//返回影响行数
if(x>0)
{
Response.Write("删除成功!");
}//如果大于0操作成功,小于等于0符合没有条件的行
cmd.Dispose();释放资源
conn.Close();//关闭连接
C#连sql增删改查
数据库名 hospital
表名 office
字段 officeName(vachar(30)),officeType(vachar(30)),department(vachar(30))
代码:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Data.SqlClient;
namespace Test
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
//声明连接
private SqlConnection conn;
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.TextBox textBox2;
private System.Windows.Forms.TextBox textBox3;
private System.Windows.Forms.Button button2;
private System.Windows.Forms.DataGrid dataGrid1;
private System.Windows.Forms.Button button4;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.button1 = new System.Windows.Forms.Button();
this.textBox1 = new System.Windows.Forms.TextBox();
this.textBox2 = new System.Windows.Forms.TextBox();
this.textBox3 = new System.Windows.Forms.TextBox();
this.button2 = new System.Windows.Forms.Button();
this.dataGrid1 = new System.Windows.Forms.DataGrid();
this.button4 = new System.Windows.Forms.Button();
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(56, 56);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "新增";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(152, 272);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(120, 21);
this.textBox1.TabIndex = 2;
this.textBox1.Text = "";
//
// textBox2
//
this.textBox2.Location = new System.Drawing.Point(304, 272);
this.textBox2.Name = "textBox2";
this.textBox2.Size = new System.Drawing.Size(120, 21);
this.textBox2.TabIndex = 2;
this.textBox2.Text = "";
//
// textBox3
//
this.textBox3.Location = new System.Drawing.Point(464, 272);
this.textBox3.Name = "textBox3";
this.textBox3.Size = new System.Drawing.Size(120, 21);
this.textBox3.TabIndex = 2;
this.textBox3.Text = "";
//
// button2
//
this.button2.Location = new System.Drawing.Point(56, 104);
this.button2.Name = "button2";
this.button2.TabIndex = 0;
this.button2.Text = "修改";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// dataGrid1
//
this.dataGrid1.DataMember = "";
this.dataGrid1.HeaderForeColor = System.Drawing.SystemColors.ControlText;
this.dataGrid1.Location = new System.Drawing.Point(152, 16);
this.dataGrid1.Name = "dataGrid1";
this.dataGrid1.Size = new System.Drawing.Size(504, 248);
this.dataGrid1.TabIndex = 1;
this.dataGrid1.CurrentCellChanged += new System.EventHandler(this.dataGrid1_CurrentCellChanged_1);
//
// button4
//
this.button4.Location = new System.Drawing.Point(56, 152);
this.button4.Name = "button4";
this.button4.TabIndex = 0;
this.button4.Text = "删除";
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(664, 318);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.button1);
this.Controls.Add(this.textBox2);
this.Controls.Add(this.textBox3);
this.Controls.Add(this.button2);
this.Controls.Add(this.dataGrid1);
this.Controls.Add(this.button4);
this.Name = "Form1";
this.Text = "Form1";
this.Load += new System.EventHandler(this.Form1_Load);
((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).EndInit();
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void btn_lj_Click(object sender, System.EventArgs e)
{
//连接串
string str="Server=(local);uid=sa;pwd=;database=hospital";
//连接
conn=new SqlConnection(str);
//sql
string sql="select officeName as 科室名称 ,officeType as 科别, "+
"department as 所属部门 from office ";
//适配器
SqlDataAdapter sqldap=new SqlDataAdapter(sql,this.conn);
//内存表
DataTable dt=new DataTable();
//填充
sqldap.Fill(dt);
//数据绑定
this.dataGrid1.DataSource=dt;
}
//刷新方法
public void myreflush()
{
string sql="select officeName as 科室名称 ,officeType as 科别, "+
"department as 所属部门 from office ";
SqlDataAdapter sqldap=new SqlDataAdapter(sql,this.conn);
DataTable dt=new DataTable();
sqldap.Fill(dt);
this.dataGrid1.DataSource=dt;
} private void button1_Click(object sender, System.EventArgs e)
{
//新增
if(this.conn.State==ConnectionState.Closed)
{
this.conn.Open();
}
string sql="insert into office values('"+this.textBox1.Text+"','"+this.textBox2.Text+"','"+this.textBox3.Text+"')";
SqlCommand sqlcmd=new SqlCommand(sql,this.conn);
int i=sqlcmd.ExecuteNonQuery();
if(i!=0)
{
MessageBox.Show("Ok");
this.myreflush();
}
else
{
MessageBox.Show("No");
}
}
//连动
private void dataGrid1_CurrentCellChanged_1(object sender, System.EventArgs e)
{
//获取当前选择行数
int iCount=this.dataGrid1.CurrentCell.RowNumber;
//数据来源
DataTable dt=(DataTable)this.dataGrid1.DataSource;
try
{
this.textBox1.Text=Convert.ToString(dt.Rows[iCount]["科室名称"]);
this.textBox2.Text=Convert.ToString(dt.Rows[iCount]["科别"]);
this.textBox3.Text=Convert.ToString(dt.Rows[iCount]["所属部门"]);
}
catch(System.IndexOutOfRangeException)
{
MessageBox.Show("此行不存在");
}
}
private void Form1_Load(object sender, System.EventArgs e)
{
//连接串
string str="Server=(local);uid=sa;pwd=;database=hospital";
//连接
conn=new SqlConnection(str);
//sql
string sql="select officeName as 科室名称 ,officeType as 科别, "+
"department as 所属部门 from office ";
//适配器
SqlDataAdapter sqldap=new SqlDataAdapter(sql,this.conn);
//内存表
DataTable dt=new DataTable();
//填充
sqldap.Fill(dt);
//数据绑定
this.dataGrid1.DataSource=dt;
}
private void button4_Click(object sender, System.EventArgs e)
{
//删除
DialogResult dr=MessageBox.Show("是否真的删除"+this.textBox1.Text,"消息",MessageBoxButtons.OKCancel);
if(dr==DialogResult.OK)
{
if(this.conn.State==ConnectionState.Closed)
{
this.conn.Open();
}
string sql="delete from office where officeName='"+this.textBox1.Text+"'";
SqlCommand sqlcmd=new SqlCommand(sql,this.conn);
int i=sqlcmd.ExecuteNonQuery();
if(i!=0)
{
MessageBox.Show("OK");
}
else
{
MessageBox.Show("No");
}
this.myreflush();
}
}
private void button2_Click(object sender, System.EventArgs e)
{
//修改
if(this.conn.State==ConnectionState.Closed)
{
this.conn.Open();
}
string sql="update office set officeType='"+this.textBox2.Text+"',department='"+this.textBox3.Text+"'"
+"where officeName='"+this.textBox1.Text+"'";
SqlCommand sqlcmd=new SqlCommand(sql,this.conn);
int i=sqlcmd.ExecuteNonQuery();
if(i!=0)
{
MessageBox.Show("OK");
this.myreflush();
}
else
{
MessageBox.Show("No");
}
}}
}