我建立了SqlDataAdapter1、sqlConnection1、dataView1,怎么样把这些数据显示在Form1上面?(附代码)

wtoeb 2003-10-16 09:40:35
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;

namespace WindowsApplication2
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class Form1 : System.Windows.Forms.Form
{
private System.Windows.Forms.MainMenu mainMenu1;
private System.Windows.Forms.MenuItem menuItem1;
private System.Windows.Forms.Button button1;
private System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1;
private System.Data.SqlClient.SqlCommand sqlSelectCommand1;
private System.Data.SqlClient.SqlCommand sqlInsertCommand1;
private System.Data.SqlClient.SqlConnection sqlConnection1;
private System.Data.DataView dataView1;
/// <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 Form Designer generated code
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.mainMenu1 = new System.Windows.Forms.MainMenu();
this.menuItem1 = new System.Windows.Forms.MenuItem();
this.button1 = new System.Windows.Forms.Button();
this.sqlDataAdapter1 = new System.Data.SqlClient.SqlDataAdapter();
this.sqlSelectCommand1 = new System.Data.SqlClient.SqlCommand();
this.sqlInsertCommand1 = new System.Data.SqlClient.SqlCommand();
this.sqlConnection1 = new System.Data.SqlClient.SqlConnection();
this.dataView1 = new System.Data.DataView();
((System.ComponentModel.ISupportInitialize)(this.dataView1)).BeginInit();
this.SuspendLayout();
//
// mainMenu1
//
this.mainMenu1.MenuItems.AddRange(new System.Windows.Forms.MenuItem[] {
this.menuItem1});
//
// menuItem1
//
this.menuItem1.Index = 0;
this.menuItem1.Text = "数据处理";
this.menuItem1.Click += new System.EventHandler(this.button1_Click);
//
// button1
//
this.button1.Location = new System.Drawing.Point(328, 288);
this.button1.Name = "button1";
this.button1.Size = new System.Drawing.Size(56, 26);
this.button1.TabIndex = 1;
this.button1.Text = "新窗口";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// sqlDataAdapter1
//
this.sqlDataAdapter1.InsertCommand = this.sqlInsertCommand1;
this.sqlDataAdapter1.SelectCommand = this.sqlSelectCommand1;
this.sqlDataAdapter1.TableMappings.AddRange(new System.Data.Common.DataTableMapping[] {
new System.Data.Common.DataTableMapping("Table", "Admin", new System.Data.Common.DataColumnMapping[] {
new System.Data.Common.DataColumnMapping("OSKEY", "OSKEY"),
new System.Data.Common.DataColumnMapping("ID", "ID"),
new System.Data.Common.DataColumnMapping("UserName", "UserName"),
new System.Data.Common.DataColumnMapping("PassWD", "PassWD")})});
//
// sqlSelectCommand1
//
this.sqlSelectCommand1.CommandText = "SELECT OSKEY, ID, UserName, PassWD FROM Admin";
this.sqlSelectCommand1.Connection = this.sqlConnection1;
//
// sqlInsertCommand1
//
this.sqlInsertCommand1.CommandText = "INSERT INTO Admin(OSKEY, ID, UserName, PassWD) VALUES (@OSKEY, @ID, @UserName, @P" +
"assWD); SELECT OSKEY, ID, UserName, PassWD FROM Admin";
this.sqlInsertCommand1.Connection = this.sqlConnection1;
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@OSKEY", System.Data.SqlDbType.NVarChar, 255, "OSKEY"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@ID", System.Data.SqlDbType.Int, 4, "ID"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@UserName", System.Data.SqlDbType.NVarChar, 50, "UserName"));
this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@PassWD", System.Data.SqlDbType.NVarChar, 50, "PassWD"));
//
// sqlConnection1
//
this.sqlConnection1.ConnectionString = "data source=GYITDOTCOM;persist security info=False;user id=sa;workstation id=GYIT" +
"DOTCOM;packet size=4096";
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.ClientSize = new System.Drawing.Size(736, 357);
this.Controls.AddRange(new System.Windows.Forms.Control[] {
this.button1});
this.Menu = this.mainMenu1;
this.Name = "Form1";
this.Text = "Form1";
((System.ComponentModel.ISupportInitialize)(this.dataView1)).EndInit();
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new Form1());
}
private void button1_Click(object sender, System.EventArgs e)
{
Form2 myform=new Form2();
myform.Show();
}
}
}
...全文
97 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
Inyoureyes 2003-10-16
  • 打赏
  • 举报
回复
sorry,别忘了关闭,conn.Close();
Inyoureyes 2003-10-16
  • 打赏
  • 举报
回复
你贴的代码好长,我看得不是很仔细,一般datagrid显示数据的步骤是
1、建立并打开连接
SqlConnection conn = new SqlConnection("连接字符")
conn.open();
2、生成数据源
string connstr = "select * from....."
SqlDataAdapter da = new SqlDataAdapter(connstr,conn);
//这里就是生DataSet
DataSet ds = new DataSet();
da.Fill(ds,"tblname");
//给表格设置数据源,帮定表格
DataGrid.DataSource = ds.Tables["tblname"].DefaultView;
DataGrid.DataBind();
你自己对一对,应该没问题
wtoeb 2003-10-16
  • 打赏
  • 举报
回复
要怎么生成数据集?dataSet1?
HNU 2003-10-16
  • 打赏
  • 举报
回复
你有没有生成数据库的 数据集dataSet11 ?
wtoeb 2003-10-16
  • 打赏
  • 举报
回复
对对对,是DataGrid,我加上去了,怎么也不会显示数据?
HNU 2003-10-16
  • 打赏
  • 举报
回复
dataView1 ?????

是 DataGrid 吧?

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧