首页数据库调用数据库,数据库如何调用

调用数据库,数据库如何调用

编程之家2023-10-22104次浏览

大家好,关于调用数据库很多朋友都还不太明白,不过没关系,因为今天小编就来为大家分享关于数据库如何调用的知识点,相信应该可以解决大家的一些困惑和问题,如果碰巧可以解决您的问题,还望关注下本站哦,希望对各位有所帮助!

调用数据库,数据库如何调用

调用数据库的问题

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

private void Form1_Load(object sender, System.EventArgs e)

{

for(int i= dataGrid1.Controls.Count-1;i>=0;i--)//循环表格

{

Control ctr= dataGrid1.Controls[i];

调用数据库,数据库如何调用

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

{

dataGrid1.Controls.RemoveAt(i);//删除

}

}

sqlDataAdapter1.Fill(this.dataset11.Flight);

调用数据库,数据库如何调用

}

private Dataset1.FlightRow currentRow;

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

{

this.dataGrid1.Select(dataGrid1.CurrentRowIndex);//选中整行

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

currentRow=(Dataset1.FlightRow)view.Row;

this.txtcode.Text=currentRow.FlightCode;//选中的行在文本框中显示

this.txtstart.Text=currentRow.Source;

this.txtover.Text=currentRow.Destination;

}

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

using System;

using System.Drawing;

using System.Collections;

using System.ComponentModel;

using System.Windows.Forms;

using System.Data;

namespace Flight

{

///<summary>

/// Form1的摘要说明。

///</summary>

public class Form1: System.Windows.Forms.Form

{

private System.Windows.Forms.DataGrid dataGrid1;

private System.Windows.Forms.TextBox txtcode;

private System.Windows.Forms.TextBox txtstart;

private System.Windows.Forms.TextBox txtover;

private System.Windows.Forms.Button button1;

private System.Windows.Forms.Button button2;

private System.Windows.Forms.Button button3;

private System.Windows.Forms.Button button4;

private System.Data.SqlClient.SqlDataAdapter sqlDataAdapter1;

private System.Data.SqlClient.SqlCommand sqlSelectCommand1;

private System.Data.SqlClient.SqlCommand sqlInsertCommand1;

private System.Data.SqlClient.SqlCommand sqlUpdateCommand1;

private System.Data.SqlClient.SqlCommand sqlDeleteCommand1;

private System.Data.SqlClient.SqlConnection conn;

private Flight.Dataset1 dataset11;

private System.Windows.Forms.DataGridTableStyle dataGridTableStyle1;

private System.Windows.Forms.DataGridTextBoxColumn dataGridTextBoxColumn1;

private System.Windows.Forms.DataGridTextBoxColumn dataGridTextBoxColumn2;

private System.Windows.Forms.DataGridTextBoxColumn dataGridTextBoxColumn4;

///<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.dataGrid1= new System.Windows.Forms.DataGrid();

this.dataset11= new Flight.Dataset1();

this.dataGridTableStyle1= new System.Windows.Forms.DataGridTableStyle();

this.dataGridTextBoxColumn1= new System.Windows.Forms.DataGridTextBoxColumn();

this.dataGridTextBoxColumn2= new System.Windows.Forms.DataGridTextBoxColumn();

this.dataGridTextBoxColumn4= new System.Windows.Forms.DataGridTextBoxColumn();

this.txtcode= new System.Windows.Forms.TextBox();

this.txtstart= new System.Windows.Forms.TextBox();

this.txtover= new System.Windows.Forms.TextBox();

this.button1= new System.Windows.Forms.Button();

this.button2= new System.Windows.Forms.Button();

this.button3= new System.Windows.Forms.Button();

this.button4= new System.Windows.Forms.Button();

this.sqlDataAdapter1= new System.Data.SqlClient.SqlDataAdapter();

this.sqlDeleteCommand1= new System.Data.SqlClient.SqlCommand();

this.conn= new System.Data.SqlClient.SqlConnection();

this.sqlInsertCommand1= new System.Data.SqlClient.SqlCommand();

this.sqlSelectCommand1= new System.Data.SqlClient.SqlCommand();

this.sqlUpdateCommand1= new System.Data.SqlClient.SqlCommand();

((System.ComponentModel.ISupportInitialize)(this.dataGrid1)).BeginInit();

((System.ComponentModel.ISupportInitialize)(this.dataset11)).BeginInit();

this.SuspendLayout();

//

// dataGrid1

//

this.dataGrid1.DataMember="Flight";

this.dataGrid1.DataSource= this.dataset11;

this.dataGrid1.HeaderForeColor= System.Drawing.SystemColors.ControlText;

this.dataGrid1.Location= new System.Drawing.Point(8, 8);

this.dataGrid1.Name="dataGrid1";

this.dataGrid1.ReadOnly= true;

this.dataGrid1.SelectionForeColor= System.Drawing.Color.Brown;

this.dataGrid1.Size= new System.Drawing.Size(312, 208);

this.dataGrid1.TabIndex= 0;

this.dataGrid1.TableStyles.AddRange(new System.Windows.Forms.DataGridTableStyle[]{

this.dataGridTableStyle1});

this.dataGrid1.Navigate+= new System.Windows.Forms.NavigateEventHandler(this.dataGrid1_Navigate);

this.dataGrid1.CurrentCellChanged+= new System.EventHandler(this.dataGrid1_CurrentCellChanged);

//

// dataset11

//

this.dataset11.DataSetName="Dataset1";

this.dataset11.Locale= new System.Globalization.CultureInfo("en-US");

//

// dataGridTableStyle1

//

this.dataGridTableStyle1.DataGrid= this.dataGrid1;

this.dataGridTableStyle1.GridColumnStyles.AddRange(new System.Windows.Forms.DataGridColumnStyle[]{

this.dataGridTextBoxColumn1,

this.dataGridTextBoxColumn2,

this.dataGridTextBoxColumn4});

this.dataGridTableStyle1.HeaderForeColor= System.Drawing.SystemColors.ControlText;

this.dataGridTableStyle1.MappingName="Flight";

//

// dataGridTextBoxColumn1

//

this.dataGridTextBoxColumn1.Format="";

this.dataGridTextBoxColumn1.FormatInfo= null;

this.dataGridTextBoxColumn1.HeaderText="航班号";

this.dataGridTextBoxColumn1.MappingName="FlightCode";

this.dataGridTextBoxColumn1.Width= 75;

//

// dataGridTextBoxColumn2

//

this.dataGridTextBoxColumn2.Format="";

this.dataGridTextBoxColumn2.FormatInfo= null;

this.dataGridTextBoxColumn2.HeaderText="起点";

this.dataGridTextBoxColumn2.MappingName="Source";

this.dataGridTextBoxColumn2.Width= 75;

//

// dataGridTextBoxColumn4

//

this.dataGridTextBoxColumn4.Format="";

this.dataGridTextBoxColumn4.FormatInfo= null;

this.dataGridTextBoxColumn4.HeaderText="终点";

this.dataGridTextBoxColumn4.MappingName="Destination";

this.dataGridTextBoxColumn4.Width= 75;

//

// txtcode

//

this.txtcode.Location= new System.Drawing.Point(336, 24);

this.txtcode.Name="txtcode";

this.txtcode.Size= new System.Drawing.Size(168, 21);

this.txtcode.TabIndex= 1;

this.txtcode.Text="";

//

// txtstart

//

this.txtstart.Location= new System.Drawing.Point(336, 72);

this.txtstart.Name="txtstart";

this.txtstart.Size= new System.Drawing.Size(168, 21);

this.txtstart.TabIndex= 2;

this.txtstart.Text="";

//

// txtover

//

this.txtover.Location= new System.Drawing.Point(336, 120);

this.txtover.Name="txtover";

this.txtover.Size= new System.Drawing.Size(168, 21);

this.txtover.TabIndex= 3;

this.txtover.Text="";

//

// button1

//

this.button1.Location= new System.Drawing.Point(344, 152);

this.button1.Name="button1";

this.button1.TabIndex= 4;

this.button1.Text="添加";

this.button1.Click+= new System.EventHandler(this.button1_Click);

//

// button2

//

this.button2.Location= new System.Drawing.Point(344, 192);

this.button2.Name="button2";

this.button2.TabIndex= 5;

this.button2.Text="删除";

this.button2.Click+= new System.EventHandler(this.button2_Click);

//

// button3

//

this.button3.Location= new System.Drawing.Point(432, 152);

this.button3.Name="button3";

this.button3.TabIndex= 6;

this.button3.Text="修改";

this.button3.Click+= new System.EventHandler(this.button3_Click);

//

// button4

//

this.button4.Location= new System.Drawing.Point(432, 192);

this.button4.Name="button4";

this.button4.TabIndex= 5;

this.button4.Text="退出";

this.button4.Click+= new System.EventHandler(this.button4_Click);

//

// sqlDataAdapter1

//

this.sqlDataAdapter1.DeleteCommand= this.sqlDeleteCommand1;

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","Flight", new System.Data.Common.DataColumnMapping[]{

new System.Data.Common.DataColumnMapping("FlightCode","FlightCode"),

new System.Data.Common.DataColumnMapping("Source","Source"),

new System.Data.Common.DataColumnMapping("Destination","Destination")})});

this.sqlDataAdapter1.UpdateCommand= this.sqlUpdateCommand1;

//

// sqlDeleteCommand1

//

this.sqlDeleteCommand1.CommandText="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)";

this.sqlDeleteCommand1.Connection= this.conn;

this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_FlightCode", System.Data.SqlDbType.VarChar, 10, System.Data.ParameterDirection.Input, false,((System.Byte)(0)),((System.Byte)(0)),"FlightCode", System.Data.DataRowVersion.Original, null));

this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_Destination", System.Data.SqlDbType.VarChar, 50, System.Data.ParameterDirection.Input, false,((System.Byte)(0)),((System.Byte)(0)),"Destination", System.Data.DataRowVersion.Original, null));

this.sqlDeleteCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_Source", System.Data.SqlDbType.VarChar, 50, System.Data.ParameterDirection.Input, false,((System.Byte)(0)),((System.Byte)(0)),"Source", System.Data.DataRowVersion.Original, null));

//

// conn

//

this.conn.ConnectionString="workstation id=PC210;packet size=4096;integrated security=SSPI;initial catalog=Ai"+

"r;persist security info=False";

//

// sqlInsertCommand1

//

this.sqlInsertCommand1.CommandText="INSERT INTO Flight(FlightCode, Source, Destination) VALUES(@FlightCode,@Source,"+

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

"Code=@FlightCode)";

this.sqlInsertCommand1.Connection= this.conn;

this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@FlightCode", System.Data.SqlDbType.VarChar, 10,"FlightCode"));

this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Source", System.Data.SqlDbType.VarChar, 50,"Source"));

this.sqlInsertCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Destination", System.Data.SqlDbType.VarChar, 50,"Destination"));

//

// sqlSelectCommand1

//

this.sqlSelectCommand1.CommandText="SELECT FlightCode, Source, Destination FROM Flight";

this.sqlSelectCommand1.Connection= this.conn;

//

// sqlUpdateCommand1

//

this.sqlUpdateCommand1.CommandText=@"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)";

this.sqlUpdateCommand1.Connection= this.conn;

this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@FlightCode", System.Data.SqlDbType.VarChar, 10,"FlightCode"));

this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Source", System.Data.SqlDbType.VarChar, 50,"Source"));

this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Destination", System.Data.SqlDbType.VarChar, 50,"Destination"));

this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_FlightCode", System.Data.SqlDbType.VarChar, 10, System.Data.ParameterDirection.Input, false,((System.Byte)(0)),((System.Byte)(0)),"FlightCode", System.Data.DataRowVersion.Original, null));

this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_Destination", System.Data.SqlDbType.VarChar, 50, System.Data.ParameterDirection.Input, false,((System.Byte)(0)),((System.Byte)(0)),"Destination", System.Data.DataRowVersion.Original, null));

this.sqlUpdateCommand1.Parameters.Add(new System.Data.SqlClient.SqlParameter("@Original_Source", System.Data.SqlDbType.VarChar, 50, System.Data.ParameterDirection.Input, false,((System.Byte)(0)),((System.Byte)(0)),"Source", System.Data.DataRowVersion.Original, null));

//

// Form1

//

this.AutoScaleBaseSize= new System.Drawing.Size(6, 14);

this.ClientSize= new System.Drawing.Size(528, 229);

this.Controls.Add(this.button3);

this.Controls.Add(this.button2);

this.Controls.Add(this.button1);

this.Controls.Add(this.txtover);

this.Controls.Add(this.txtstart);

this.Controls.Add(this.txtcode);

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();

((System.ComponentModel.ISupportInitialize)(this.dataset11)).EndInit();

this.ResumeLayout(false);

}

#endregion

///<summary>

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

///</summary>

[STAThread]

static void Main()

{

Application.Run(new Form1());

}

private void button4_Click(object sender, System.EventArgs e)

{

Application.Exit();

}

private void Form1_Load(object sender, System.EventArgs e)

{

for(int i= dataGrid1.Controls.Count-1;i>=0;i--)

{

Control ctr= dataGrid1.Controls[i];

if(ctr is DataGridTextBox)

{

dataGrid1.Controls.RemoveAt(i);

}

}

sqlDataAdapter1.Fill(this.dataset11.Flight);

}

private Dataset1.FlightRow currentRow;

private void dataGrid1_CurrentCellChanged(object sender, System.EventArgs e)

{

this.dataGrid1.Select(dataGrid1.CurrentRowIndex);//选中整行

DataRowView view=(DataRowView)this.BindingContext[dataset11,"Flight"].Current;

currentRow=(Dataset1.FlightRow)view.Row;

this.txtcode.Text=currentRow.FlightCode;

this.txtstart.Text=currentRow.Source;

this.txtover.Text=currentRow.Destination;

}

private void button3_Click(object sender, System.EventArgs e)

{

currentRow.FlightCode=this.txtcode.Text;

currentRow.Source=this.txtstart.Text;

currentRow.Destination= this.txtover.Text;

sqlDataAdapter1.Update(this.dataset11.Flight);

}

private void button1_Click(object sender, System.EventArgs e)

{

DataRow newrow= dataset11.Tables[0].NewRow();

newrow[0]=this.txtcode.Text;

newrow[1]=this.txtstart.Text;

newrow[2]=this.txtover.Text;

dataset11.Flight.Rows.Add(newrow);

sqlDataAdapter1.Update(this.dataset11.Flight);

}

private void button2_Click(object sender, System.EventArgs e)

{

currentRow.Delete();

sqlDataAdapter1.Update(this.dataset11.Flight);

MessageBox.Show("删除成功!!!");

}

private void dataGrid1_Navigate(object sender, System.Windows.Forms.NavigateEventArgs 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

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

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

数据库如何调用

1、先分析数据库需要保存哪些信息,还要考虑用什么类型的字段,当然,刚开始时没经验可能会有问题,多试几次。

基本上一个信息就是一个字段,如行政区是一个字段,地理位置是另一个字段,因为你的这个图片上所表示出现的信息比较单一,所以可以考虑只用一个数据表,但是如果想要让功能更加多样化的话,最好是多使用几个有关联的表,相互之间的操作才能给用户更多的体验。

2、数据库的操作,其实就是Select、insert、updata之类的。

数据库的连接之类的很简单,就是一句连接代码而已,其它的就是一个数据集的操作,根据用户的选择反馈得到不同的结果,查询之类的也就是这个意思

3、asp的设计,可以用DWMX之类的软件,做起来简单点。

找几个简单的留言本的asp源码的来看看就差不多了。

-----------------------------------------------------

顺便说一句,asp其实不建议学习了,要么用C#用asp.net,要么学习PHP,以后扩展空间大一些!

怎么调用mysql数据库

可以使用MySQL的内置函数SELECT来调用MySQL数据库,例如:

SELECT* FROM [table_name];

其中[table_name]表示要调用的表名。

NineData是一款非常有特色的数据库SQL开发产品,对MySQL常用功能支持非常完整,支持多种连接和访问云数据库的方式,对阿里云、腾讯云、华为云、AWS等都有比较好的支持。另外,也适配国内比较流行的PolarDB、GaussDB、TDSQL等数据库。对于新用户NineData还会赠送两个示例数据库,供用户使用。

VB怎么调用数据库数据

VB可将Sql结构化查询语句赋值Ado(ActiveX数据对象)或ADO Data控件,访问和操作数据库。

ADO(ActiveX数据对象),这项新的数据访问技术的特性包括:更简单的对象模型;与其它 Microsoft和非 Microsoft的技术更好的集成;为本地和远程数据数据提供的通用接口;可远程访问的和断开的记录集;用户可访问的数据绑定接口;以及层次结构的记录集。

ADO Data控件,这是一种新的、OLEDB识别的数据源控件,其功能与内部的 Data控件和 Remote Data控件十分相似,通过这种方式允许用户用最少的代码来创建数据库应用程序。

Visual Basic 6.0中,已经可以将任何 ADO/OLE DB数据源绑定到任何 ADO/OLE DB数据使用者上。在运行时,可以设置控件的 DataSource属性来将控件动态地绑定到数据源。可以创建用作数据源和数据使用者的类,并且通过新的 BindingsCollection对象将这些类绑定在一起。可以创建与 ADO数据控件相似的、用作数据源的用户控件。还可以创建与 DataGrid控件相似的、复杂绑定的用户控件。

以下是经ADO(ActiveX数据对象)访问Access数据库的实例代码:

DimcnnAsNewConnection

DimrsAsNewRecordset

DimsqlAsString

DimARR

cnn.Open"Provider=Microsoft.Jet.OLEDB.4.0;DataSource=C:\wdOld.mdb;PersistSecurityInfo=False"

sql="select故障名称fromguzhang_bm"

rs.Opensql,cnn

Combo1.Text="故障名称"

DoWhileNotrs.EOF

Combo1.AddItemrs("故障名称")

rs.MoveNext'打开表

Loop

rs.Close

cnn.Close

以下是使用ADO Data控件访问Access数据库德实例代码:

Adodc1.ConnectionString="Provider=Microsoft.Jet.OLEDB.4.0;DataSource="&App.Path&"\wd.mdb;PersistSecurityInfo=False"

Adodc1.RecordSource="select*fromjishijilu"'whereshijianlike'%8112%'Andgyh_riqilike'%10%'"

Adodc1.Refresh

SetMSHFlexGrid1.DataSource=Adodc1

MSHFlexGrid1.Row=1

MSHFlexGrid1.ColSel=MSHFlexGrid1.Cols-1

好了,关于调用数据库和数据库如何调用的问题到这里结束啦,希望可以解决您的问题哈!

数据库系统工程师真题(数据库系统工程师考试内容有哪些)frp 服务器 frp如何加用户名