调用数据库的问题

调用数据库的问题,第1张

新建一个数据集-----连接数据库!---绑定表格-----Dataset1xsd

private void Form1_Load(object sender, SystemEventArgs e)

{

for(int i = dataGrid1ControlsCount -1;i>=0;i--)//循环表格

{

Control ctr = dataGrid1Controls[i];

if(ctr is DataGridTextBox)//删除表格中的单元格

{

dataGrid1ControlsRemoveAt(i);//删除

}

}

sqlDataAdapter1Fill(thisdataset11Flight);

}

private Dataset1FlightRow currentRow;

private void dataGrid1_CurrentCellChanged(object sender, SystemEventArgs e)//表格改变事件CurrentCellChanged

{

thisdataGrid1Select(dataGrid1CurrentRowIndex);//选中整行

DataRowView view=(DataRowView)thisBindingContext//绑定数据行视图[dataset11,"Flight"]Current;

currentRow=(Dataset1FlightRow)viewRow;

thistxtcodeText=currentRowFlightCode;//选中的行在文本框中显示

thistxtstartText=currentRowSource;

thistxtoverText=currentRowDestination;

}

--------------------完整的例子--------------------------------------

using System;

using SystemDrawing;

using SystemCollections;

using SystemComponentModel;

using SystemWindowsForms;

using SystemData;

namespace Flight

{

/// <summary>

/// Form1 的摘要说明。

/// </summary>

public class Form1 : SystemWindowsFormsForm

{

private SystemWindowsFormsDataGrid dataGrid1;

private SystemWindowsFormsTextBox txtcode;

private SystemWindowsFormsTextBox txtstart;

private SystemWindowsFormsTextBox txtover;

private SystemWindowsFormsButton button1;

private SystemWindowsFormsButton button2;

private SystemWindowsFormsButton button3;

private SystemWindowsFormsButton button4;

private SystemDataSqlClientSqlDataAdapter sqlDataAdapter1;

private SystemDataSqlClientSqlCommand sqlSelectCommand1;

private SystemDataSqlClientSqlCommand sqlInsertCommand1;

private SystemDataSqlClientSqlCommand sqlUpdateCommand1;

private SystemDataSqlClientSqlCommand sqlDeleteCommand1;

private SystemDataSqlClientSqlConnection conn;

private FlightDataset1 dataset11;

private SystemWindowsFormsDataGridTableStyle dataGridTableStyle1;

private SystemWindowsFormsDataGridTextBoxColumn dataGridTextBoxColumn1;

private SystemWindowsFormsDataGridTextBoxColumn dataGridTextBoxColumn2;

private SystemWindowsFormsDataGridTextBoxColumn dataGridTextBoxColumn4;

/// <summary>

/// 必需的设计器变量。

/// </summary>

private SystemComponentModelContainer components = null;

public Form1()

{

//

// Windows 窗体设计器支持所必需的

//

InitializeComponent();

//

// TODO: 在 InitializeComponent 调用后添加任何构造函数代码

//

}

/// <summary>

/// 清理所有正在使用的资源。

/// </summary>

protected override void Dispose( bool disposing )

{

if( disposing )

{

if (components != null)

{

componentsDispose();

}

}

baseDispose( disposing );

}

#region Windows 窗体设计器生成的代码

/// <summary>

/// 设计器支持所需的方法 - 不要使用代码编辑器修改

/// 此方法的内容。

/// </summary>

private void InitializeComponent()

{

thisdataGrid1 = new SystemWindowsFormsDataGrid();

thisdataset11 = new FlightDataset1();

thisdataGridTableStyle1 = new SystemWindowsFormsDataGridTableStyle();

thisdataGridTextBoxColumn1 = new SystemWindowsFormsDataGridTextBoxColumn();

thisdataGridTextBoxColumn2 = new SystemWindowsFormsDataGridTextBoxColumn();

thisdataGridTextBoxColumn4 = new SystemWindowsFormsDataGridTextBoxColumn();

thistxtcode = new SystemWindowsFormsTextBox();

thistxtstart = new SystemWindowsFormsTextBox();

thistxtover = new SystemWindowsFormsTextBox();

thisbutton1 = new SystemWindowsFormsButton();

thisbutton2 = new SystemWindowsFormsButton();

thisbutton3 = new SystemWindowsFormsButton();

thisbutton4 = new SystemWindowsFormsButton();

thissqlDataAdapter1 = new SystemDataSqlClientSqlDataAdapter();

thissqlDeleteCommand1 = new SystemDataSqlClientSqlCommand();

thisconn = new SystemDataSqlClientSqlConnection();

thissqlInsertCommand1 = new SystemDataSqlClientSqlCommand();

thissqlSelectCommand1 = new SystemDataSqlClientSqlCommand();

thissqlUpdateCommand1 = new SystemDataSqlClientSqlCommand();

((SystemComponentModelISupportInitialize)(thisdataGrid1))BeginInit();

((SystemComponentModelISupportInitialize)(thisdataset11))BeginInit();

thisSuspendLayout();

//

// dataGrid1

//

thisdataGrid1DataMember = "Flight";

thisdataGrid1DataSource = thisdataset11;

thisdataGrid1HeaderForeColor = SystemDrawingSystemColorsControlText;

thisdataGrid1Location = new SystemDrawingPoint(8, 8);

thisdataGrid1Name = "dataGrid1";

thisdataGrid1ReadOnly = true;

thisdataGrid1SelectionForeColor = SystemDrawingColorBrown;

thisdataGrid1Size = new SystemDrawingSize(312, 208);

thisdataGrid1TabIndex = 0;

thisdataGrid1TableStylesAddRange(new SystemWindowsFormsDataGridTableStyle[] {

thisdataGridTableStyle1});

thisdataGrid1Navigate += new SystemWindowsFormsNavigateEventHandler(thisdataGrid1_Navigate);

thisdataGrid1CurrentCellChanged += new SystemEventHandler(thisdataGrid1_CurrentCellChanged);

//

// dataset11

//

thisdataset11DataSetName = "Dataset1";

thisdataset11Locale = new SystemGlobalizationCultureInfo("en-US");

//

// dataGridTableStyle1

//

thisdataGridTableStyle1DataGrid = thisdataGrid1;

thisdataGridTableStyle1GridColumnStylesAddRange(new SystemWindowsFormsDataGridColumnStyle[] {

thisdataGridTextBoxColumn1,

thisdataGridTextBoxColumn2,

thisdataGridTextBoxColumn4});

thisdataGridTableStyle1HeaderForeColor = SystemDrawingSystemColorsControlText;

thisdataGridTableStyle1MappingName = "Flight";

//

// dataGridTextBoxColumn1

//

thisdataGridTextBoxColumn1Format = "";

thisdataGridTextBoxColumn1FormatInfo = null;

thisdataGridTextBoxColumn1HeaderText = "航班号";

thisdataGridTextBoxColumn1MappingName = "FlightCode";

thisdataGridTextBoxColumn1Width = 75;

//

// dataGridTextBoxColumn2

//

thisdataGridTextBoxColumn2Format = "";

thisdataGridTextBoxColumn2FormatInfo = null;

thisdataGridTextBoxColumn2HeaderText = "起点";

thisdataGridTextBoxColumn2MappingName = "Source";

thisdataGridTextBoxColumn2Width = 75;

//

// dataGridTextBoxColumn4

//

thisdataGridTextBoxColumn4Format = "";

thisdataGridTextBoxColumn4FormatInfo = null;

thisdataGridTextBoxColumn4HeaderText = "终点";

thisdataGridTextBoxColumn4MappingName = "Destination";

thisdataGridTextBoxColumn4Width = 75;

//

// txtcode

//

thistxtcodeLocation = new SystemDrawingPoint(336, 24);

thistxtcodeName = "txtcode";

thistxtcodeSize = new SystemDrawingSize(168, 21);

thistxtcodeTabIndex = 1;

thistxtcodeText = "";

//

// txtstart

//

thistxtstartLocation = new SystemDrawingPoint(336, 72);

thistxtstartName = "txtstart";

thistxtstartSize = new SystemDrawingSize(168, 21);

thistxtstartTabIndex = 2;

thistxtstartText = "";

//

// txtover

//

thistxtoverLocation = new SystemDrawingPoint(336, 120);

thistxtoverName = "txtover";

thistxtoverSize = new SystemDrawingSize(168, 21);

thistxtoverTabIndex = 3;

thistxtoverText = "";

//

// button1

//

thisbutton1Location = new SystemDrawingPoint(344, 152);

thisbutton1Name = "button1";

thisbutton1TabIndex = 4;

thisbutton1Text = "添加";

thisbutton1Click += new SystemEventHandler(thisbutton1_Click);

//

// button2

//

thisbutton2Location = new SystemDrawingPoint(344, 192);

thisbutton2Name = "button2";

thisbutton2TabIndex = 5;

thisbutton2Text = "删除";

thisbutton2Click += new SystemEventHandler(thisbutton2_Click);

//

// button3

//

thisbutton3Location = new SystemDrawingPoint(432, 152);

thisbutton3Name = "button3";

thisbutton3TabIndex = 6;

thisbutton3Text = "修改";

thisbutton3Click += new SystemEventHandler(thisbutton3_Click);

//

// button4

//

thisbutton4Location = new SystemDrawingPoint(432, 192);

thisbutton4Name = "button4";

thisbutton4TabIndex = 5;

thisbutton4Text = "退出";

thisbutton4Click += new SystemEventHandler(thisbutton4_Click);

//

// sqlDataAdapter1

//

thissqlDataAdapter1DeleteCommand = thissqlDeleteCommand1;

thissqlDataAdapter1InsertCommand = thissqlInsertCommand1;

thissqlDataAdapter1SelectCommand = thissqlSelectCommand1;

thissqlDataAdapter1TableMappingsAddRange(new SystemDataCommonDataTableMapping[] {

new SystemDataCommonDataTableMapping("Table", "Flight", new SystemDataCommonDataColumnMapping[] {

new SystemDataCommonDataColumnMapping("FlightCode", "FlightCode"),

new SystemDataCommonDataColumnMapping("Source", "Source"),

new SystemDataCommonDataColumnMapping("Destination", "Destination")})});

thissqlDataAdapter1UpdateCommand = thissqlUpdateCommand1;

//

// sqlDeleteCommand1

//

thissqlDeleteCommand1CommandText = "DELETE FROM Flight WHERE (FlightCode = @Original_FlightCode) AND (Destination = @" +

"Original_Destination OR @Original_Destination IS NULL AND Destination IS NULL) A" +

"ND (Source = @Original_Source OR @Original_Source IS NULL AND Source IS NULL)";

thissqlDeleteCommand1Connection = thisconn;

thissqlDeleteCommand1ParametersAdd(new SystemDataSqlClientSqlParameter("@Original_FlightCode", SystemDataSqlDbTypeVarChar, 10, SystemDataParameterDirectionInput, false, ((SystemByte)(0)), ((SystemByte)(0)), "FlightCode", SystemDataDataRowVersionOriginal, null));

thissqlDeleteCommand1ParametersAdd(new SystemDataSqlClientSqlParameter("@Original_Destination", SystemDataSqlDbTypeVarChar, 50, SystemDataParameterDirectionInput, false, ((SystemByte)(0)), ((SystemByte)(0)), "Destination", SystemDataDataRowVersionOriginal, null));

thissqlDeleteCommand1ParametersAdd(new SystemDataSqlClientSqlParameter("@Original_Source", SystemDataSqlDbTypeVarChar, 50, SystemDataParameterDirectionInput, false, ((SystemByte)(0)), ((SystemByte)(0)), "Source", SystemDataDataRowVersionOriginal, null));

//

// conn

//

thisconnConnectionString = "workstation id=PC210;packet size=4096;integrated security=SSPI;initial catalog=Ai" +

"r;persist security info=False";

//

// sqlInsertCommand1

//

thissqlInsertCommand1CommandText = "INSERT INTO Flight(FlightCode, Source, Destination) VALUES (@FlightCode, @Source," +

" @Destination); SELECT FlightCode, Source, Destination FROM Flight WHERE (Flight" +

"Code = @FlightCode)";

thissqlInsertCommand1Connection = thisconn;

thissqlInsertCommand1ParametersAdd(new SystemDataSqlClientSqlParameter("@FlightCode", SystemDataSqlDbTypeVarChar, 10, "FlightCode"));

thissqlInsertCommand1ParametersAdd(new SystemDataSqlClientSqlParameter("@Source", SystemDataSqlDbTypeVarChar, 50, "Source"));

thissqlInsertCommand1ParametersAdd(new SystemDataSqlClientSqlParameter("@Destination", SystemDataSqlDbTypeVarChar, 50, "Destination"));

//

// sqlSelectCommand1

//

thissqlSelectCommand1CommandText = "SELECT FlightCode, Source, Destination FROM Flight";

thissqlSelectCommand1Connection = thisconn;

//

// sqlUpdateCommand1

//

thissqlUpdateCommand1CommandText = @"UPDATE Flight SET FlightCode = @FlightCode, Source = @Source, Destination = @Destination WHERE (FlightCode = @Original_FlightCode) AND (Destination = @Original_Destination OR @Original_Destination IS NULL AND Destination IS NULL) AND (Source = @Original_Source OR @Original_Source IS NULL AND Source IS NULL); SELECT FlightCode, Source, Destination FROM Flight WHERE (FlightCode = @FlightCode)";

thissqlUpdateCommand1Connection = thisconn;

thissqlUpdateCommand1ParametersAdd(new SystemDataSqlClientSqlParameter("@FlightCode", SystemDataSqlDbTypeVarChar, 10, "FlightCode"));

thissqlUpdateCommand1ParametersAdd(new SystemDataSqlClientSqlParameter("@Source", SystemDataSqlDbTypeVarChar, 50, "Source"));

thissqlUpdateCommand1ParametersAdd(new SystemDataSqlClientSqlParameter("@Destination", SystemDataSqlDbTypeVarChar, 50, "Destination"));

thissqlUpdateCommand1ParametersAdd(new SystemDataSqlClientSqlParameter("@Original_FlightCode", SystemDataSqlDbTypeVarChar, 10, SystemDataParameterDirectionInput, false, ((SystemByte)(0)), ((SystemByte)(0)), "FlightCode", SystemDataDataRowVersionOriginal, null));

thissqlUpdateCommand1ParametersAdd(new SystemDataSqlClientSqlParameter("@Original_Destination", SystemDataSqlDbTypeVarChar, 50, SystemDataParameterDirectionInput, false, ((SystemByte)(0)), ((SystemByte)(0)), "Destination", SystemDataDataRowVersionOriginal, null));

thissqlUpdateCommand1ParametersAdd(new SystemDataSqlClientSqlParameter("@Original_Source", SystemDataSqlDbTypeVarChar, 50, SystemDataParameterDirectionInput, false, ((SystemByte)(0)), ((SystemByte)(0)), "Source", SystemDataDataRowVersionOriginal, null));

//

// Form1

//

thisAutoScaleBaseSize = new SystemDrawingSize(6, 14);

thisClientSize = new SystemDrawingSize(528, 229);

thisControlsAdd(thisbutton3);

thisControlsAdd(thisbutton2);

thisControlsAdd(thisbutton1);

thisControlsAdd(thistxtover);

thisControlsAdd(thistxtstart);

thisControlsAdd(thistxtcode);

thisControlsAdd(thisdataGrid1);

thisControlsAdd(thisbutton4);

thisName = "Form1";

thisText = "Form1";

thisLoad += new SystemEventHandler(thisForm1_Load);

((SystemComponentModelISupportInitialize)(thisdataGrid1))EndInit();

((SystemComponentModelISupportInitialize)(thisdataset11))EndInit();

thisResumeLayout(false);

}

#endregion

/// <summary>

/// 应用程序的主入口点。

/// </summary>

[STAThread]

static void Main()

{

ApplicationRun(new Form1());

}

private void button4_Click(object sender, SystemEventArgs e)

{

ApplicationExit();

}

private void Form1_Load(object sender, SystemEventArgs e)

{

for(int i = dataGrid1ControlsCount -1;i>=0;i--)

{

Control ctr = dataGrid1Controls[i];

if(ctr is DataGridTextBox)

{

dataGrid1ControlsRemoveAt(i);

}

}

sqlDataAdapter1Fill(thisdataset11Flight);

}

private Dataset1FlightRow currentRow;

private void dataGrid1_CurrentCellChanged(object sender, SystemEventArgs e)

{

thisdataGrid1Select(dataGrid1CurrentRowIndex);//选中整行

DataRowView view=(DataRowView)thisBindingContext[dataset11,"Flight"]Current;

currentRow=(Dataset1FlightRow)viewRow;

thistxtcodeText=currentRowFlightCode;

thistxtstartText=currentRowSource;

thistxtoverText=currentRowDestination;

}

private void button3_Click(object sender, SystemEventArgs e)

{

currentRowFlightCode=thistxtcodeText;

currentRowSource=thistxtstartText;

currentRowDestination= thistxtoverText;

sqlDataAdapter1Update(thisdataset11Flight);

}

private void button1_Click(object sender, SystemEventArgs e)

{

DataRow newrow = dataset11Tables[0]NewRow();

newrow[0]=thistxtcodeText;

newrow[1]=thistxtstartText;

newrow[2]=thistxtoverText;

dataset11FlightRowsAdd(newrow);

sqlDataAdapter1Update(thisdataset11Flight);

}

private void button2_Click(object sender, SystemEventArgs e)

{

currentRowDelete();

sqlDataAdapter1Update(thisdataset11Flight);

MessageBoxShow("删除成功!!!");

}

private void dataGrid1_Navigate(object sender, SystemWindowsFormsNavigateEventArgs ne)

{

}

}

}

----------------------------数据库--------------------------------

create database Air

go

use Air

go

create table Flight

(

FlightCode varchar(10) primary key,

Source varchar(50),

Destination varchar(50)

)

go

insert into Flight values('AF0001','厦门','上海')

insert into Flight values('BH8888','厦门','北京')

insert into Flight values('DR8988','北京','上海')

insert into Flight values('UI6766','福州','上海')

insert into Flight values('FG5432','北京','西安')

insert into Flight values('QW4555','兰州','上海')

insert into Flight values('ES4333','北京','成都')

insert into Flight values('NB5666','南昌','上海')

select from Flight

-----------------------还要建设一个数据集----------------

选择项目---添加新建项-----数据集(不要改名)---从服务器资源管理器(左边的数据库)把上面的表格拉到里面搞定!!!

WebBrowserNavigate 方法:将指定位置的文档加载到 WebBrowser 控件中。

你可以看看

>

GridView

连接的代码是后台写的还是前台直接 *** 作生成的

我一般修改的做法是传递当前列的标识到另一页面

然后进行修改

在GridView添加HyperLinkField 列

DataNavigateUrlFields为要绑定的数据库字段

DataNavigateUrlFormatString为要跳转的页面ID={0}后边的参数

然后在另外一页面用RequestQueryString["id"]来接受传过来的字段 进行读取

<asp:HyperLinkField HeaderText="编辑" Text="编辑" DataNavigateUrlFields="ID" DataNavigateUrlFormatString="UpdateProductaspxID={0}">

<ItemStyle HorizontalAlign="Center" Wrap="False" />

<HeaderStyle HorizontalAlign="Center" Wrap="False" />

</asp:HyperLinkField>

估计你的做法是在GridView 列中放的TextBox 然后修改TextBox后希望数据库也修改。

可以用TextBox的Onchange事件,当TextBox的值被改变,激发此事件

//点击订单负责人跳转分配页面,带参数a是指定订单分配人

hl1NavigateUrl = "~/BasicSystem/NewTsmFile/OrderDistriButeInfoaspxOrderId=" + OrderIdText + "&Charge=a";

//点击分配跳转分配页面,带登录人所属的部门的参数

hl2NavigateUrl = "~/BasicSystem/NewTsmFile/OrderDistriButeInfoaspxOrderId=" + OrderIdText + "&department=" + Session["Unit"] + "";

这是后台的,我去给你找前台啊

<asp:HyperLink ID="HyperLink3" runat="server" NavigateUrl='<%# Eval("Id","~/BasicSystem/NewTsmFile/OrderManageAddaspxid={0}") %>'

这是前台的

TCHAR url[1024];

int n=100;

_stprintf(url,TEXT(">

<asp:HyperLink ID="HyperLink2" runat="server" NavigateUrl='<%# "show-jobaspxjobid="+Eval("id")+"&cid="+ServerUrlEncode((Eval("c_uname1"))ToString()Trim()) %>'

Target="_blank" Text='<%# getLen(Eval("c_jobName", "{0}"),12) %>'

ToolTip='<%# (Eval("c_jobName", "{0}"))ToString()Trim() %>'></asp:HyperLink>

我相信实例最有说服力,LZ你看看这段代码,我相信你一定会明白怎么做的。。

DataNavigateUrlFields 属性里可以写多个参数,,然后在DataNavigateUrlFormatString 里用{0}{1}{2}{3}可以写很多

使用Eval函数获取当前行的字段值 ,如下所示,jf_id为id字段名称

NavigateUrl='<%#"testaspxguid=" + Eval("jf_id")ToString() %>'

以上就是关于调用数据库的问题全部的内容,包括:调用数据库的问题、navigate在java中是什么意思、C#绑定数据库并且提交修改数据等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/sjk/9792754.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-02
下一篇 2023-05-02

发表评论

登录后才能评论

评论列表(0条)

保存