Reputation: 347
Sir,
Made an activity page
and declared System.Data.SqlClient
to connect it with MSSQL
. Then I have declared SqlConnection
class, but it showing me error as The type or namespace name 'SqlConnection' could not be found (are you missing a using directive or an assembly reference?)
below is .xaml code :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minWidth="25px"
android:minHeight="25px">
<TextView
android:text="Enter First Name"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView1" />
<EditText
android:inputType="textPersonName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editText1" />
<TextView
android:text="Enter Last Name"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView2" />
<EditText
android:inputType="textPersonName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editText2" />
<TextView
android:text="Enter Contact No"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView3" />
<EditText
android:inputType="textPersonName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/editText3" />
<Button
android:text="Save Data"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/saveDatabtn1" />
</LinearLayout>
.cs code:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using System.Text.RegularExpressions;
using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
namespace MobAppDB
{
[Activity(Label = "InsertData_Act")]
public class InsertData_Act : Activity
{
static string Constr = "Data Source=HP-PC;Initial Catalog=UserMobileData;User Id=sa;Password=sa@123;";
//Connection Class
SqlConnection con = new SqlConnection(Constr);
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
//Initialize Controls
SetContentView(Resource.Layout.InsertData);
Button butt = FindViewById<Button>(Resource.Id.saveDatabtn1);
butt.Click += saveDatabtn1_Click;
}
private void saveDatabtn1_Click(object sender, System.EventArgs e)
{
using (SqlConnection conn = new SqlConnection(constr))
{
}
}
}
}
Upvotes: 0
Views: 619
Reputation: 1197
Mysql support for Xamarin.android ? I don't think you are really looking MYSql db which has to be access remotely as none of the platform supports it .
I think what you are looking for is SQLite db not Mysql.
Sqlite is light weight database made for mobile platform.
and the class name you are looking for is.
SQLiteConnection.
More on this you can read from https://developer.xamarin.com/recipes/android/data/databases/sqlite/
Realm db is another best approach for offline data storage which requires no SQL knowledge.
But you are really looking to support Mysql db in Xamarin.android. Go for it . I won't recommend it at all
https://components.xamarin.com/view/mysql-plugin
Upvotes: 1