forked from hussien89aa/AndroidTutorialForBeginners
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCSharpSqlServer.cs
More file actions
91 lines (79 loc) · 3.12 KB
/
CSharpSqlServer.cs
File metadata and controls
91 lines (79 loc) · 3.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// set config
/* add this in Web config
<system.web>
<webServices>
<protocols>
<add name="HttpGet"/>
</protocols>
</webServices>
</system.web>
*/
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = true)]
public void Login(string UserName, string Password) ///Tag =0 devele 1 is add
{
JavaScriptSerializer ser = new JavaScriptSerializer();
string UserID="";
string Message="";
try
{
SqlDataReader reader;
using (SqlConnection connection = new SqlConnection(DBConnection.ConnectionString))
{
SqlCommand cmd = new SqlCommand("SELECT UserID FROM Users where UserName=@UserName and Password=@Password ");
cmd.CommandType = CommandType.Text;
cmd.Connection = connection;
cmd.Parameters.AddWithValue("@UserName", UserName);
cmd.Parameters.AddWithValue("@Password", Password);
connection.Open();
reader = cmd.ExecuteReader();
while (reader.Read())
{
UserID = reader.GetInt32(0);
}
reader.Close();
connection.Close();
}
}
catch (Exception ex)
{
Message = " cannot access to the data";
}
var jsonData = new
{ Message=Message
UserID = UserID,
};
HttpContext.Current.Response.Write(ser.Serialize(jsonData));
}
//login
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json, UseHttpGet = true)]
public void Login(string UserName, string Password) ///Tag =0 devele 1 is add
{
JavaScriptSerializer ser = new JavaScriptSerializer();
string Message = "";
try
{
using (SqlConnection connection = new SqlConnection("Data Source=localhost;Initial Catalog=Users;Integrated Security=True"))
{
SqlCommand cmd = new SqlCommand("Insert into Login(UserName,Password)values(@UserName,@Password) ");
cmd.CommandType = CommandType.Text;
cmd.Connection = connection;
cmd.Parameters.AddWithValue("@UserName", UserName);
cmd.Parameters.AddWithValue("@Password", Password);
connection.Open();
cmd.ExecuteNonQuery();
connection.Close();
Message = " data is added";
}
}
catch (Exception ex)
{
Message = " cannot access to the data" ;
}
var jsonData = new
{
Message = Message
};
HttpContext.Current.Response.Write(ser.Serialize(jsonData));
}