Unity3d connection to SQLServer Database

Source: Internet
Author: User
Tags i18n

Before writing code, you must:

1. Introduce the assembly System. Data. dll in C: Program FilesUnityEditorDataMonolibmonounity (My unity is installed on the C drive by default ).

2. Copy the System. Data. dll file to the Current Unity project file, that is, under the Assets folder.

Although it can be run in unity, it still cannot be connected after it is published as web or exe, which requires three other dll files I18N. dll, I18N. west. dll, I18N. CJK. dll, these three files and System. data. the dll is in the same directory. To put the four dll files together in the Assets file and publish them, you can connect to the database.

Public class ConnectionSqlserver: MonoBehaviour

{

Void Start ()

{

// Connection string

SqlConnection connstr = new SqlConnection ("Data Source = ..; Initial Catalog = Exam; uid = sa; pwd = 123 ");

SqlCommand cmd = new SqlCommand ();

Cmd. Connection = connstr;

Cmd. CommandType = System. Data. CommandType. Text;

// Set the SQL connection statement

Cmd. CommandText = "select * from Users ";

SqlDataAdapter sda = new SqlDataAdapter (cmd );

Sda. SelectCommand. Connection. Open ();

String strtemp = sda. SelectCommand. ExecuteScalar (). ToString ();

Sda. SelectCommand. Connection. Close ();

Print ("database connected! "+ Strtemp );

}

}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.