这个例子,由于在ADO.net入门已经专门学了,再次进行复习 一下。 主要掌握连接字串的情况。 过程就是: 1、引用System.Data.SqlClient。而Access中引用 的是System.Data.OleDB.所以是有区别的 2、相关连接、适配器、数据集。 3、DataGridView绑定显示。 连接
这个例子,由于在ADO.net入门已经专门学了,再次进行复习 一下。
主要掌握连接字串的情况。
过程就是:
1、引用System.Data.SqlClient。而Access中引用 的是System.Data.OleDB.所以是有区别的
2、相关连接、适配器、数据集。
3、DataGridView绑定显示。
连接字串如下:
Data Source IP地址或计算名(数据库所在位置的),如果是本地计算机可以用(local)或直接用.来代替,或者本地IP:127.0.0.1代替。
Initial Catalog 数据库名
Integrated Security 安全连接情况(真或假),若是Windows验证方式则为真,若是用户名及密码登录则为假。
User ID 上项为假时,提供用户名
Password ...................,提供密码。
因此,就上面五项就瓮中保证了字串。
如:本地上Sales数据库,用户名为sa,密码为123456,各项之间用分号隔开,则字串为如下:
Data Source=(Local);Initial Catalog=Sales;Integrated Security=False;User ID=sa;Password=123456;
因为在本地所以用了(Local),还可直接用点号来代替,同时若本地计算机名为Zheng,还可用Zheng。
下面正题:
在SQlServer2012中建立一个Sales数据库,再在其中建立一个表grade.情况如下:
打开VS2012,建立一个窗体,添加一DataGridView控件,双击窗体建立代码:
Imports System.Data.SqlClient Public Class Form1 Dim cn As SqlConnection Dim da As SqlDataAdapter Dim ds As DataSet Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load Dim cnStr As String = "Data Source=(local);Initial Catalog=Sales;Integrated Security=False;User ID=sa;Password=123456;" cn = New SqlConnection(cnStr) da = New SqlDataAdapter("select * from grade", cn) ds = New DataSet() da.Fill(ds, "grade") DataGridView1.DataSource = ds.Tables("grade") End Sub End Class
运行结果如下:
本例只须记住SQlserver连接时,连接字串的“5个”关键值就行了。
===================================================
细节:
da.Fill(ds, "xxx")
DataGridView1.DataSource = ds.Tables("xxx")
这两句的xxx表示表名, 这个表名与原数据库的表名可以不一样。
因为这里是DataSet结果集中的表了,不是原数据库中的,用户自己定义的表名,因此可以任意命名,但这两名应保持一致。
一般都不会自己 再定义,如果有特殊情况时,这个自定义情况就可发挥作用。

MySQL'sBLOBissuitableforstoringbinarydatawithinarelationaldatabase,whileNoSQLoptionslikeMongoDB,Redis,andCassandraofferflexible,scalablesolutionsforunstructureddata.BLOBissimplerbutcanslowdownperformancewithlargedata;NoSQLprovidesbetterscalabilityand

ToaddauserinMySQL,use:CREATEUSER'username'@'host'IDENTIFIEDBY'password';Here'showtodoitsecurely:1)Choosethehostcarefullytocontrolaccess.2)SetresourcelimitswithoptionslikeMAX_QUERIES_PER_HOUR.3)Usestrong,uniquepasswords.4)EnforceSSL/TLSconnectionswith

ToavoidcommonmistakeswithstringdatatypesinMySQL,understandstringtypenuances,choosetherighttype,andmanageencodingandcollationsettingseffectively.1)UseCHARforfixed-lengthstrings,VARCHARforvariable-length,andTEXT/BLOBforlargerdata.2)Setcorrectcharacters

MySQloffersechar, Varchar, text, Anddenumforstringdata.usecharforfixed-Lengthstrings, VarcharerForvariable-Length, text forlarger text, AndenumforenforcingdataAntegritywithaetofvalues.

Optimizing MySQLBLOB requests can be done through the following strategies: 1. Reduce the frequency of BLOB query, use independent requests or delay loading; 2. Select the appropriate BLOB type (such as TINYBLOB); 3. Separate the BLOB data into separate tables; 4. Compress the BLOB data at the application layer; 5. Index the BLOB metadata. These methods can effectively improve performance by combining monitoring, caching and data sharding in actual applications.

Mastering the method of adding MySQL users is crucial for database administrators and developers because it ensures the security and access control of the database. 1) Create a new user using the CREATEUSER command, 2) Assign permissions through the GRANT command, 3) Use FLUSHPRIVILEGES to ensure permissions take effect, 4) Regularly audit and clean user accounts to maintain performance and security.

ChooseCHARforfixed-lengthdata,VARCHARforvariable-lengthdata,andTEXTforlargetextfields.1)CHARisefficientforconsistent-lengthdatalikecodes.2)VARCHARsuitsvariable-lengthdatalikenames,balancingflexibilityandperformance.3)TEXTisidealforlargetextslikeartic

Best practices for handling string data types and indexes in MySQL include: 1) Selecting the appropriate string type, such as CHAR for fixed length, VARCHAR for variable length, and TEXT for large text; 2) Be cautious in indexing, avoid over-indexing, and create indexes for common queries; 3) Use prefix indexes and full-text indexes to optimize long string searches; 4) Regularly monitor and optimize indexes to keep indexes small and efficient. Through these methods, we can balance read and write performance and improve database efficiency.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 English version
Recommended: Win version, supports code prompts!

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft

SublimeText3 Linux new version
SublimeText3 Linux latest version

Dreamweaver CS6
Visual web development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment
