<appSettings>
<add key="con" value="server=.uid=sapwd=database=Blog"></add>
</appSettings>
要用的话就这样写
con=new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["con"])
其实就是key value结构!
android 链接mysql数据库实例:package com.hl
import java.sql.DriverManager
import java.sql.ResultSet
import com.mysql.jdbc.Connection
import com.mysql.jdbc.Statement
import android.app.Activity
import android.os.Bundle
import android.view.View
import android.view.View.OnClickListener
import android.widget.Button
import android.widget.TextView
public class AndroidMsql extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState)
setContentView(R.layout.main)
Button btn=(Button)findViewById(R.id.btn)
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
sqlCon()
}
})
}
private void mSetText(String str){
TextView txt=(TextView)findViewById(R.id.txt)
txt.setText(str)
}
private void sqlCon(){
try {
Class.forName("com.mysql.jdbc.Driver")
} catch (Exception e) {
e.printStackTrace()
}
try {
String url ="jdbc:mysql://192.168.142.128:3306/mysql?user=zzfeihua&password=12345&useUnicode=true&characterEncoding=UTF-8"//链接数据库语句
Connection conn= (Connection) DriverManager.getConnection(url)//链接数据库
Statement stmt=(Statement) conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_UPDATABLE)
String sql="select * from user"//查询user表语句
ResultSet rs=stmt.executeQuery(sql)//执行查询
StringBuilder str=new StringBuilder()
while(rs.next()){
str.append(rs.getString(1)+"\n")
}
mSetText(str.toString())
rs.close()
stmt.close()
conn.close()
} catch (Exception e) {
e.printStackTrace()
}
}
}
不过eclipse老是提示:
warning: Ignoring InnerClasses attribute for an anonymous inner class that doesn't come with an associated EnclosingMethod attribute. (This class was probably produced by a broken compiler.)
其实就是写一个连接类,然后调用就可以了;using System
using System.Data
using System.Configuration
using System.Web
using System.Web.Security
using System.Web.UI
using System.Web.UI.WebControls
using System.Web.UI.WebControls.WebParts
using System.Web.UI.HtmlControls
using System.Data.SqlClient
using System.IO
/// <summary>
/// DBConnection 的摘要说明
/// </summary>
public class DBconnection
{
SqlConnection conn = null
SqlCommand cmd = new SqlCommand()
public DBconnection()
{
conn = new SqlConnection("server=.database=university_dbuid=sapwd=123")
}
public SqlConnection getconn()
{
if (conn.State == ConnectionState.Closed)
conn.Open()//打开连接
return conn
}
//关闭连接
public void clear()
{
if (conn.State == ConnectionState.Open)
conn.Close()
}
}
然后在你要用连接的页面的.cs页面调用就可以
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)