| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 3991 人关注过本帖
标题:[求助]与数据库连接的代码写在什么地方?
只看楼主 加入收藏
菜鸟呵
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2007-4-16
收藏
 问题点数:0 回复次数:13 
[求助]与数据库连接的代码写在什么地方?

我在界面上只添加了一个按钮和一个文本框,想实现的功能是:点击按钮后将SQL数据库中的数据显示在文本框中。
请教:连接数据库的代码往哪里填写?
谢谢!
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;

namespace Form1
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.Button button1;
private System.Windows.Forms.TextBox textBox1;
/// <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.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(48, 176);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(88, 32);
this.button1.TabIndex = 0;
this.button1.Text = "button1";
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(32, 48);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(88, 21);
this.textBox1.TabIndex = 1;
this.textBox1.Text = "textBox1";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(292, 270);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}
#endregion
}
}

搜索更多相关主题的帖子: 数据库 代码 
2007-04-16 13:30
jacklee
Rank: 7Rank: 7Rank: 7
来 自:XAplus
等 级:贵宾
威 望:32
帖 子:1769
专家分:104
注 册:2006-11-3
收藏
得分:0 
初使化的地方可以。也可以通过按钮等方式来连

XAplus!
讨论群:51090447
删吧删吧,把我的号给删了!
2007-04-16 13:39
飙马
Rank: 5Rank: 5
来 自:马里亚纳
等 级:贵宾
威 望:15
帖 子:779
专家分:280
注 册:2007-3-28
收藏
得分:0 
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
在它的后面写!

IT精英如同彩票:平凡的人像5块也中不到一样普遍,努力一点你中了5元保了个本。奖金越高,机率也就越小,付出的也越多,盖茨如同500万一样稀有。虽然每天忙碌而平凡,但我努力成为精英,做梦中了500万。
2007-04-16 15:18
IPV6
Rank: 1
等 级:新手上路
威 望:2
帖 子:265
专家分:0
注 册:2006-9-7
收藏
得分:0 

写一个连接函数
比如:
public Form1()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();

//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
public void DataBind()
{
-------代码
}


2007-04-16 15:27
菜鸟呵
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2007-4-16
收藏
得分:0 
我已经建了数据库,想把让Form1窗体第一次出现时,显示表1中的第一条记录。以下是我的代码
public void Diaplay()
{
string Str="Persist Security Info=False;Initial Catalog=Northwind;Data Source=localhost;Integrated Security=SSPI;";
SqlConnection Con = new SqlConnection (Str);
Con.Open ();
SqlCommand Com = new SqlCommand ();
Com.CommandType = CommandType.Text ;
string str="select Name from 1;";
Com.CommandText =str;
Com.Connection =Con;
SqlDataReader Reader = Com.ExecuteReader ();
Reader.Read ();
this.textBox1 .Text =Reader.GetString (0);
Reader.Close ();
Con.Close ();
}
运行没有错误,却仍然不能显示记录。
各位大哥,帮帮忙啊
2007-04-16 16:28
liuminghui
Rank: 6Rank: 6
等 级:贵宾
威 望:20
帖 子:2882
专家分:0
注 册:2007-1-26
收藏
得分:0 

我以前用的方法,你看看吧
DataSet ds = new DataSet();

SqlConnection conn = new SqlConnection("Data Source=localhost;Initial Catalog=student;Integrated Security=True");
SqlDataAdapter daAuthors = new SqlDataAdapter("Select ID ,sno,sname,sex From student ", conn);
conn.Open();
daAuthors.Fill(ds, "student");

// conn.Close() '在填充完ds后关闭连接,接着对ds进行操作
dataGrid1.DataSource = ds.Tables[0];//vb.net上使用这样的语句:ds.Tables("student");

conn.Close();


海鸽 is My Lover!!
2007-04-16 16:30
bygg
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:乖乖的心中
等 级:版主
威 望:241
帖 子:13555
专家分:3076
注 册:2006-10-23
收藏
得分:0 
以下是引用菜鸟呵在2007-4-16 16:28:31的发言:
我已经建了数据库,想把让Form1窗体第一次出现时,显示表1中的第一条记录。以下是我的代码
public void Diaplay()
{
string Str="Persist Security Info=False;Initial Catalog=Northwind;Data Source=localhost;Integrated Security=SSPI;";
SqlConnection Con = new SqlConnection (Str);
Con.Open ();
SqlCommand Com = new SqlCommand ();
Com.CommandType = CommandType.Text ;
string str="select Name from 1;"; //你的数据表就叫 1
Com.CommandText =str;
Com.Connection =Con;
SqlDataReader Reader = Com.ExecuteReader ();
Reader.Read (); //这里应该加个判断..
this.textBox1 .Text =Reader.GetString (0);
Reader.Close ();
Con.Close ();
}
运行没有错误,却仍然不能显示记录。
各位大哥,帮帮忙啊


飘过~~
2007-04-16 17:07
菜鸟呵
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2007-4-16
收藏
得分:0 
回复:(bygg)以下是引用菜鸟呵在2007-4-16 16:28:31...

敢问楼上这位大哥,加什么判断啊?我买的书上就是这样写的,我实在不明白啊

Reader.Read (); //这里应该加个判断..



我建的表名就是1

2007-04-16 19:37
年少无知
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2006-5-11
收藏
得分:0 

连接数据库字符串错了~我不习惯用VS生成的连接字符串
server=(localhost);uid="你的登陆名";pwd="密码";DataBase=数据库名;

还有你这个查询语句"select Name from 1".你说你想查第一条记录."select top 1 from 1"这样就显示第一条记录,如果你只想查询"Name"字段下第一条记录"select top 1 Name from 1"
注:top 后面那个1是你想要显示的行数..from 后面的那个1是你建的表名.

2007-04-16 20:42
菜鸟呵
Rank: 1
等 级:新手上路
帖 子:8
专家分:0
注 册:2007-4-16
收藏
得分:0 
server=(localhost);uid="你的登陆名";pwd="密码";DataBase=数据库名;

能告诉我完整的代码怎么写吗?书上只有我写的那样子的啊

谢谢
2007-04-16 20:53
快速回复:[求助]与数据库连接的代码写在什么地方?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.012766 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved