Is there a way to use sql bulk insert without using datatable from generic list? - datatable

I have a generic list contains about six and a half million. I would like to insert this generic list to database by using sql bulk insert. But a datatable is necessary for using sql bulk insert. Convert generic list to datatable takes too much time. So how can I use sql bulk insert without using datatable ?
EDIT:
I use MSSQL SERVER and .NET FrameWork
I would like to just use it with a generic list.

I created a custom datareader that have been inherited IDataReader. I have achieved like below :
public class CustomDataReader<T> : IDataReader
{
private readonly IEnumerator<T> list;
private readonly List<PropertyInfo> properties = new List<PropertyInfo>();
public CustomDataReader(IEnumerable<T> list)
{
this.list = list.GetEnumerator();
var props = typeof(T).GetProperties();
foreach (var propertyInfo in props)
{
properties.Add(propertyInfo);
}
}
#region IDataReader Members
public void Close()
{
list.Dispose();
}
public int Depth
{
get { throw new NotImplementedException(); }
}
public DataTable GetSchemaTable()
{
throw new NotImplementedException();
}
public bool IsClosed
{
get { throw new NotImplementedException(); }
}
public bool NextResult()
{
throw new NotImplementedException();
}
public bool Read()
{
return list.MoveNext();
}
public int RecordsAffected
{
get { throw new NotImplementedException(); }
}
#endregion
#region IDisposable Members
public void Dispose()
{
Close();
}
#endregion
#region IDataRecord Members
public int FieldCount
{
get { return properties.Count; }
}
public bool GetBoolean(int i)
{
throw new NotImplementedException();
}
public byte GetByte(int i)
{
throw new NotImplementedException();
}
public long GetBytes(int i, long fieldOffset, byte[] buffer, int bufferoffset, int length)
{
throw new NotImplementedException();
}
public char GetChar(int i)
{
throw new NotImplementedException();
}
public long GetChars(int i, long fieldoffset, char[] buffer, int bufferoffset, int length)
{
throw new NotImplementedException();
}
public IDataReader GetData(int i)
{
throw new NotImplementedException();
}
public string GetDataTypeName(int i)
{
throw new NotImplementedException();
}
public DateTime GetDateTime(int i)
{
throw new NotImplementedException();
}
public decimal GetDecimal(int i)
{
throw new NotImplementedException();
}
public double GetDouble(int i)
{
throw new NotImplementedException();
}
public Type GetFieldType(int i)
{
return properties[i].PropertyType;
}
public float GetFloat(int i)
{
throw new NotImplementedException();
}
public Guid GetGuid(int i)
{
throw new NotImplementedException();
}
public short GetInt16(int i)
{
throw new NotImplementedException();
}
public int GetInt32(int i)
{
throw new NotImplementedException();
}
public long GetInt64(int i)
{
throw new NotImplementedException();
}
public string GetName(int i)
{
return properties[i].Name;
}
public int GetOrdinal(string name)
{
throw new NotImplementedException();
}
public string GetString(int i)
{
throw new NotImplementedException();
}
public object GetValue(int i)
{
return properties[i].GetValue(list.Current, null);
}
public int GetValues(object[] values)
{
throw new NotImplementedException();
}
public bool IsDBNull(int i)
{
throw new NotImplementedException();
}
public object this[string name]
{
get { throw new NotImplementedException(); }
}
public object this[int i]
{
get { throw new NotImplementedException(); }
}
#endregion
}
public static void BulkInsert<T>(this IEnumerable<T> list, string connStr, string tableName)
{
using (var reader = new CustomDataReader<T>(list))
{
using (var conn = new SqlConnection(connStr))
{
using (var sbc = new SqlBulkCopy(conn))
{
conn.Open();
sbc.DestinationTableName = tableName;
sbc.WriteToServer(reader);
conn.Close();
}
}
}
}

Related

How to load markers instantly android

I using googleMap and what i'm tring to do is to refresh markers from firebase database, and that mean i delete all the previous markers and add the new ones by using this methode
#Override
public void onKeyEntered(String key, GeoLocation location) {
try{
MyItem myItem = new MyItem(location.latitude, location.longitude);
if (!items.contains(myItem)) {
items.add(myItem);
}
Log.d("onKey","called");
}catch (ClassCastException e){
Log.d("classCastException","");
}
}
parseJsonToList methode :
private void parseJsonToList() {
itemss = clusterManagerAlgorithm.getItems();
try {
items.removeAll(itemss);
}catch (IndexOutOfBoundsException e){
Log.d("itemsDoesn't exist"," : ");
}
mClusterManager.clearItems();
mClusterManager.cluster();
mClusterManager.addItems(items);
Log.d("items"," : " + items);
}
So, my problem is that everytime I'm tring to clean and load data what happened is that all markers desapears for about 3 second to been reloaded again. How can i delete and reload data without markers dispear.
this is myItem class :
public class MyItem implements ClusterItem {
private final LatLng mPosition;
public MyItem(double lat, double lng) {
mPosition = new LatLng(lat, lng);
}
#Override
public LatLng getPosition() {
return mPosition;
}
#Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
MyItem item = (MyItem) o;
return Objects.equals(mPosition, item.mPosition);
}
#Override
public int hashCode() {
return Objects.hash(mPosition);
}
}

Unable to cast object of type 'System.Linq.EnumerableQuery to type 'Microsoft.Azure.Documents.Linq.IDocumentQuery

I have a class with the following Method and am using Moq as a Unit Testing Framework. How can I mock the following:
FeedOptions feedOptions = new FeedOptions
{
MaxItemCount = 1000
};
var query = await _storeAccessClient.CreateDocumentQueryAsync<CustomEntity>(_collectionLink, feedOptions)
.Where(c => c.DataType == _dataType)
.OrderBy(c => c.StartTime, sortOrder)
.AsDocumentQuery()
.ExecuteNextAsync<CustomEntity>();
List<CustomEntity> result = query.ToList<CustomEntity>();
Any Help is greatly Appreciated !!
All you have to do is create a wrapper around EnumerableQuery class which inherits from IQueryable and IDocumentQuery like this:
internal class MockEnumerableQuery : IDocumentQuery<JTokenEx>, IOrderedQueryable<JTokenEx>
{
public IQueryable<JTokenEx> List;
private readonly bool bypassExpressions;
public MockEnumerableQuery(EnumerableQuery<JTokenEx> List, bool bypassExpressions = true)
{
this.List = List;
this.bypassExpressions = bypassExpressions;
}
public IEnumerator<JTokenEx> GetEnumerator()
{
return List.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public Expression Expression => List.Expression;
public Type ElementType => typeof(JTokenEx);
public IQueryProvider Provider => new MockQueryProvider(this, bypassExpressions);
public void Dispose()
{
}
public Task<FeedResponse<TResult>> ExecuteNextAsync<TResult>(CancellationToken token = new CancellationToken())
{
BindingFlags flags = BindingFlags.NonPublic | BindingFlags.Instance;
FeedResponse<JToken> feed = Activator.CreateInstance(typeof(FeedResponse<JToken>),
flags,null,new Object[] { List.Select(j => (JToken) j), 0, new NameValueCollection(), false, null}, null)
as FeedResponse<JToken>;
return Task.FromResult(feed as FeedResponse<TResult>);
}
public Task<FeedResponse<dynamic>> ExecuteNextAsync(CancellationToken token = new CancellationToken())
{
throw new NotImplementedException();
}
public bool HasMoreResults { get; }
}
class MockQueryProvider : IQueryProvider
{
private readonly MockEnumerableQuery mockQuery;
private readonly bool bypassExpressions;
public MockQueryProvider(MockEnumerableQuery mockQuery, bool byPassExpressions)
{
this.mockQuery = mockQuery;
this.bypassExpressions = byPassExpressions;
}
public IQueryable CreateQuery(Expression expression)
{
throw new NotImplementedException();
}
public IQueryable<TElement> CreateQuery<TElement>(Expression expression)
{
if (!bypassExpressions)
{
mockQuery.List = mockQuery.List.Provider.CreateQuery<TElement>(expression) as IQueryable<JTokenEx>;
}
return (IQueryable<TElement>)mockQuery;
}
public object Execute(Expression expression)
{
throw new NotImplementedException();
}
public TResult Execute<TResult>(Expression expression)
{
throw new NotImplementedException();
}
}
Now where your mock is returning EnumerableQuery, you return this MockEnumerableQuery class and you should be good.

android volley JsonArrayRequest return nothing

in the below code my arrayList will be empty after JsonArrayRequest block.
I set break point at this line: "int size = arrayList.size();"
every thing is OK until "while" loop finishes. after that allayList is empty.
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.GET, json_url,(String) null,
new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
int count=0;
int responseLength = response.length();
responseLength--;
while (count<responseLength)
{
try {
JSONObject jsonObject = response.getJSONObject(count);
Contact contact = new Contact(jsonObject.getString("title"),
jsonObject.getString("email"),
jsonObject.getString("description"),
jsonObject.getString("date"),
jsonObject.getBoolean("status"));
arrayList.add(contact);
int size = arrayList.size();
count++;
} catch (JSONException e) {
e.printStackTrace();
}
}
int size = arrayList.size();
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(context,"Error....",Toast.LENGTH_SHORT).show();
error.printStackTrace();
}
}
);
int size = arrayList.size();
VolleySingleton.getmInstance(context).addToRequestQueue(jsonArrayRequest);
return arrayList;
I will show what i did using CallBack interface:
in onCreate() method:
recyclerView = (RecyclerView) findViewById(R.id.recyclerview);
recyclerView.setHasFixedSize(true);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
recyclerView.setLayoutManager(linearLayoutManager);
BackgroundTask backgroundTask = new BackgroundTask(this);
backgroundTask.getContacts(new BackgroundTask.arrayListCallBack() {
#Override
public void onSuccess(ArrayList<Contact> contacts) {
RecyclerView.Adapter adapter = new RecyclerAdapter(MainActivity.this, contacts);
recyclerView.setAdapter(adapter);
}
#Override
public void onFail(String error) {
Toast.makeText(MainActivity.this, error, Toast.LENGTH_LONG).show();
}
});
and in the BackgroundTask class:
JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Request.Method.POST, server_url, new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
int count = 0;
while (count < response.length()) {
try {
JSONObject jsonObject = response.getJSONObject(count);
Contact contact = new Contact(jsonObject.getString("name"), jsonObject.getString("section"));
contacts.add(contact);
Log.d("process request", "....."+jsonObject.getString("name"));
count++;
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(context, e.getMessage()+"\nError in Response", Toast.LENGTH_LONG).show();
}
callBack.onSuccess(contacts);
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
Toast.makeText(context, error.getMessage()+"\nError in Connection", Toast.LENGTH_LONG).show();
callBack.onFail("There's error ...");
}
});
MySingleton.getInstance(context).addToRequestQueue(jsonArrayRequest);
}
public interface arrayListCallBack {
void onSuccess(ArrayList<Contact> contacts);
void onFail(String error);
}

jdbcTemplate.batchUpdate issue

How to convert this to return a jdbcTemplate.batchUpdate?
It needs to be executed to several tables.
#Override
public int delete(int id) {
String sql = "update user set deleted=1 where user_id = ?";
Object[] params = new Object[] { id };
try {
return jdbcTemplate.update(sql, params);
} catch (IncorrectResultSizeDataAccessException e) {
throw new UserNotFoundException("User.not.found");
}
}
#Override
public void delete(List<Integer> ids) {
String sql = "update user set deleted=1 where user_id = ?";
jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter() {
#Override
public void setValues(PreparedStatement ps, int i) throws SQLException {
Integer userId = ids.get(i);
ps.setLong(1, userId );
}
#Override
public int getBatchSize() {
return ids.size();
}
});
}

Do not store password in .net membership

I use the built in membership system in a MVC 3 .net application. Later in the developement I will use an external web service for authentication. Hence I would only have to store the (unique) username in the membership system. All other user info can be retrieved through the webservice.
Therefore I wonder how to not store a password?
Don't worry about the storing of the password, just randomly generate and store a password when you create the user.
Have your account controller validate the password against the external webservice in the logon method, if its correct, then simply call FormsAuthentication.SetAuthCookie(userName, false /*persistantCookie*/), which will "login" the user :)
side note:
Have you though about how you will migrate the existing user's to the new external webservice if you only have their password hash/salts?
Not sure if I understand you correctly but I think the best solution to this is writing an custom membership provider. Basically this is just an class with an few functions overriden from the basic membership provider. Here you can implement your own logic for registering, logging in and logging out.
Found an example of an class I used an while back. Just write your own implementation. An other option is to work from your accountcontroller (like haz also mentioned) but I always tend not to implement too much logic into my controllers and let my services handle the business logic.
public class CustomMembershipProvider : MembershipProvider
{
private readonly IGenericService<User> _genericUserService;
public CustomMembershipProvider(IGenericService<User> genericUserService)
{
_genericUserService = genericUserService;
}
public CustomMembershipProvider() : this(new GenericService<User>())
{
}
public override MembershipUser CreateUser(string username, string password, string email, string passwordQuestion, string passwordAnswer, bool isApproved, object providerUserKey, out MembershipCreateStatus status)
{
throw new NotImplementedException();
}
public override bool ChangePasswordQuestionAndAnswer(string username, string password, string newPasswordQuestion, string newPasswordAnswer)
{
throw new NotImplementedException();
}
public override string GetPassword(string username, string answer)
{
throw new NotImplementedException();
}
public override bool ChangePassword(string username, string oldPassword, string newPassword)
{
throw new NotImplementedException();
}
public override string ResetPassword(string username, string answer)
{
throw new NotImplementedException();
}
public override void UpdateUser(MembershipUser user)
{
throw new NotImplementedException();
}
public override bool ValidateUser(string username, string password)
{
try
{
var encodedPassword = password.AsSha512();
var user = _genericUserService.First(u => u.Email == username && u.Password == string.Empty );
return user != null;
}
catch (Exception)
{
return false;
}
}
public override bool UnlockUser(string userName)
{
throw new NotImplementedException();
}
public override MembershipUser GetUser(object providerUserKey, bool userIsOnline)
{
throw new NotImplementedException();
}
public override MembershipUser GetUser(string username, bool userIsOnline)
{
var user = _genericUserService.First(x => x.Email.Equals(username));
var a = new MembershipUser("", user.Firstname, user.Id, user.Email, "", "", true, user.Active,
user.RegisteredOn, DateTime.Now, DateTime.Now, DateTime.Now, DateTime.Now);
return a;
}
public override string GetUserNameByEmail(string email)
{
throw new NotImplementedException();
}
public override bool DeleteUser(string username, bool deleteAllRelatedData)
{
throw new NotImplementedException();
}
public override MembershipUserCollection GetAllUsers(int pageIndex, int pageSize, out int totalRecords)
{
throw new NotImplementedException();
}
public override int GetNumberOfUsersOnline()
{
throw new NotImplementedException();
}
public override MembershipUserCollection FindUsersByName(string usernameToMatch, int pageIndex, int pageSize, out int totalRecords)
{
throw new NotImplementedException();
}
public override MembershipUserCollection FindUsersByEmail(string emailToMatch, int pageIndex, int pageSize, out int totalRecords)
{
throw new NotImplementedException();
}
public override bool EnablePasswordRetrieval
{
get { throw new NotImplementedException(); }
}
public override bool EnablePasswordReset
{
get { throw new NotImplementedException(); }
}
public override bool RequiresQuestionAndAnswer
{
get { throw new NotImplementedException(); }
}
public override string ApplicationName
{
get { throw new NotImplementedException(); }
set { throw new NotImplementedException(); }
}
public override int MaxInvalidPasswordAttempts
{
get { throw new NotImplementedException(); }
}
public override int PasswordAttemptWindow
{
get { throw new NotImplementedException(); }
}
public override bool RequiresUniqueEmail
{
get { throw new NotImplementedException(); }
}
public override MembershipPasswordFormat PasswordFormat
{
get { throw new NotImplementedException(); }
}
public override int MinRequiredPasswordLength
{
get { throw new NotImplementedException(); }
}
public override int MinRequiredNonAlphanumericCharacters
{
get { throw new NotImplementedException(); }
}
public override string PasswordStrengthRegularExpression
{
get { throw new NotImplementedException(); }
}
}

Resources